ios - 过滤后填充uitableviewcell,单元格内容始终相同

标签 ios objective-c uitableview uisearchcontroller

我正在处理一个简单的问题,即在搜索和过滤后填充 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”。 enter image description here

其余看起来也不错,名字是“Clayton Farr”,单元格内容是“Clayton Farr” enter image description here enter image description here

但是,在我的应用程序中,只有名称“Hally Wernstorm”会填充。 enter image description here

我当然不希望这种情况发生,所以我想知道我在代码中的哪个位置恢复了所有“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/

相关文章:

ios - 如何从命令行以特定语言启动 iOS 模拟器?

ios - 在 iOS 应用程序(iPhone 和 iPad)中手动选择语言

ios - MFMailComposeViewController:取消按钮不可见

ios - 是否可以在 UILabel/TextView 中输入 'hyperlink' 文本,但点击此 'hyperlink' 即可激活 segue?

ios - 如何检测 View 中的某些部分

ios - 在 Xcode/Swift 中导出 TableView 数据

ios - TableView 问题,indexPath 为零

iphone - UITableViewCell 子类中是否需要有实例变量?

iOS:如何在选项卡式 View Controller 的 View 之间获得滑动效果?

ios - Mailcore 检索文件夹和撰写功能