我正在研究一个在搜索和过滤后填充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
我分离了很多代码,所以我可以调试它,这是我搜索字母时的结果&#34; t&#34;:
我可以看到我的结果数组中有6个项目,其中包含字母&#39; t&#39;。
其余的看起来也不错,firstName是&#34; Clayton Farr&#34;细胞含量是&#34; Clayton Farr&#34;
然而,在我的应用程序中,只有名称&#34; Hally Wernstorm&#34;罢了。
我当然不希望这种情况发生,所以我想知道代码在哪里,我得到所有&#34; Hally Wernstorm&#34;回来?
我写过的一些代码:
- (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;
}
感谢任何可以提供帮助的人。
答案 0 :(得分:3)
这不是管理tableView搜索的合适方法。
您需要创建一个已过滤的数组,并在每次更改时基于它重新加载tableView。
答案 1 :(得分:1)