我正在处理一个简单的问题,即在搜索和过滤后填充 UITableViewCells。
下面是一些填充单元格的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
.... etc
if ([self.searchController isActive] && (![self.searchController.searchBar.text isEqualToString:@""])) {
NSArray *results = [self.searchResults mutableCopy];
if (results.count == 0) {
// do nothing
} else {
for (NSInteger i = 0; i < results.count; i ++) {
NSString *fullName = [results objectAtIndex:i];
((SSContactTableViewCell *)cell).fullNameLabel.text = fullName;
}
}
} else { .. etc
我分离了很多代码以便调试它,这是我搜索字母“t”时的结果:
我可以看到我的结果数组中确实有 6 个项目,所有项目都包含字母“t”。
其余看起来也不错,名字是“Clayton Farr”,单元格内容是“Clayton Farr”
但是,在我的应用程序中,只有名称“Hally Wernstorm”会填充。
我当然不希望这种情况发生,所以我想知道我在代码中的哪个位置恢复了所有“Hally Wernstorm”?
我写的一些代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (self.contactsButton.selected) {
cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCell" forIndexPath:indexPath];
if ([self.searchController isActive] && (![self.searchController.searchBar.text isEqualToString:@""])) {
NSArray *results = [self.searchResults mutableCopy];
if (results.count == 0) {
// do nothing
} else {
for (NSInteger i = 0; i < results.count; i ++) {
NSString *fullName = [results objectAtIndex:i];
((SSContactTableViewCell *)cell).fullNameLabel.text = fullName;
}
}
} else {
NSArray *sectionArray = [self.contacts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.indexTitles objectAtIndex:indexPath.section]]];
NSString *fullName = [sectionArray objectAtIndex:indexPath.row];
((SSContactTableViewCell *)cell).fullNameLabel.text = fullName;
((SSContactTableViewCell *)cell).specialtyLabel.text = [self.specialities objectAtIndex:indexPath.row];
[((SSContactTableViewCell *)cell).avatarButton setTitle:[NSString initialsOfStrings:[fullName componentsSeparatedByString:@" "]]];
}
}
else if (self.patientsButton.selected) {
cell = [tableView dequeueReusableCellWithIdentifier:@"PatientCell" forIndexPath:indexPath];
NSArray *sectionArray = [self.patients filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.indexTitles objectAtIndex:indexPath.section]]];
((SSPatientTableViewCell *)cell).fullNameLabel.text = [sectionArray objectAtIndex:indexPath.row];
}
else if (self.favouritesButton.selected) {
// cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCell" forIndexPath:indexPath];
}
return cell;
}
#pragma mark - UISearchControllerDelegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = searchController.searchBar.text;
if (searchString == nil) {
self.searchResults = [self.contacts mutableCopy];
} else {
self.searchResults = [[NSMutableArray alloc] init];
for (NSString *name in self.contacts) {
if ([name.lowercaseString containsString:searchString.lowercaseString]) {
[self.searchResults addObject:name];
}
}
}
[self.tableView reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
[self updateSearchResultsForSearchController:self.searchController];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (self.searchController.active) {
if ( self.searchResults.count == 0 && (![self.searchController.searchBar.text isEqualToString:@""]) ) {
return 0;
}
}
return 26;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.contactsButton.selected) {
NSArray *sectionArray = [self.contacts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.indexTitles objectAtIndex:section]]];
return [sectionArray count];
} else if (self.patientsButton.selected) {
NSArray *sectionArray = [self.patients filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.indexTitles objectAtIndex:section]]];
return [sectionArray count];
} else if ([self.searchController isActive] && (![self.searchController.searchBar.text isEqualToString:@""])) {
return [self.searchResults count];
}
return 6;
}
感谢任何可以提供帮助的人。
最佳答案
这不是管理 tableView 搜索的合适方式。
您需要创建一个过滤数组,并在每次更改时基于它重新加载您的 tableView。
关于ios - 过滤后填充uitableviewcell,单元格内容始终相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36339495/