ios - UITableViewCell 无法正确显示文本

标签 ios objective-c uitableview

我有一个数据列表,我从中搜索一些项目,然后选择其中一个来展开所选单元格。然后,当我清除搜索栏中的搜索文本时,我希望显示原始列表。我能够显示原始列表中的所有项目,但搜索过程中选择的单元格未更新。附上快照和代码以便更好地理解:

Original List

Search for text and select item on row 3

Clear search and display original list.

请注意,第一张图片中的第 3 行有文本“A.B. Road”,而第三张图片中的同一行使用与第二张图片中相同的单元格(它应该更新为“A.B. Road”)我正在使用自定义单元格,并且每次都尝试创建一个新单元,而不是重用现有单元。这没有帮助。

代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = [self.branchList objectAtIndex:[indexPath row]];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];

    CGSize labelSize = [cellText boundingRectWithSize:CGSizeMake(tableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cellFont} context:nil].size;

    CGFloat cellHeight = labelSize.height + 20;
    return [self.expandedCells containsObject:indexPath] ? cellHeight * 5 : cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *stateListCellId = @"stateList";

    BranchDetailsTableViewCell *stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];

    if (!stateListCell) {
        [tableView registerNib:[UINib nibWithNibName:@"BranchDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:stateListCellId];
        stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
    }

    return stateListCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.expandedCells containsObject:indexPath]) {
        [self.expandedCells removeObject:indexPath];

        [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates];
    }
    else {
        [self.activityIndicator showActivityIndicatorForView:self.navigationController.view];
        self.selectedIndexPath = indexPath;
        [self.expandedCells addObject:indexPath];
        [self getDataFromService];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(BranchDetailsTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [self displayDataOnTheCell];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    if ([self.tableView numberOfRowsInSection:0] != [self.branchListCopy count]) {
        [self.expandedCells removeAllObjects];
// Copy the original list to display.
        self.branchList = self.branchListCopy;
        [self.tableView reloadData];
    }
}

看起来它只是没有再次呈现的单元格,因为当我调试时,我确实在数组中看到“A.B. Road”,它是没有显示它的单元格。我尝试调用“[cell setNeedsDisplay]”并始终创建新单元而不是重用,但没有任何帮助。还有什么可以帮助?

谢谢!

最佳答案

我可以通过在 didSelectRowAtIndexPath 方法中将以下代码替换为 [tableview reloadData] 来使其工作,并保持其余代码不变。

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

最终工作解决方案:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = [self.branchList objectAtIndex:[indexPath row]];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];

    CGSize labelSize = [cellText boundingRectWithSize:CGSizeMake(tableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cellFont} context:nil].size;

    CGFloat cellHeight = labelSize.height + 20;
    return [self.expandedCells containsObject:indexPath] ? cellHeight * 5 : cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *stateListCellId = @"stateList";

    BranchDetailsTableViewCell *stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];

    if (!stateListCell) {
        [tableView registerNib:[UINib nibWithNibName:@"BranchDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:stateListCellId];
        stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
    }

    return stateListCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.expandedCells containsObject:indexPath]) {
        [self.expandedCells removeObject:indexPath];

        [self.tableView reloadData];
    }
    else {
        [self.activityIndicator showActivityIndicatorForView:self.navigationController.view];
        self.selectedIndexPath = indexPath;
        [self.expandedCells addObject:indexPath];
        [self getDataFromService];
    }
}

关于ios - UITableViewCell 无法正确显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570740/

相关文章:

ios - RLM 异常 : 'Primary key property ' serial' does not exist on object 'Book' Migrating to Swift 4

iPhone SDK : Image arrays?

objective-c - 基于 NSDocument 的应用程序和 NSToolbar

ios - sectionIndexTitlesForTableView 中字母之间的间距,空对象技巧不再起作用了吗?

ios - 在表格 View 单元格中加载和缓存多个 UIImageView 不起作用

ios - 如何在表格 View 单元格中使用开关?

css - jqmobi。在 ios (ipod) 上,当输入字段聚焦的内容向上移动时

ios - 尝试构建一个简单的调谐器

iphone - 行数为 0 时 UITableView 中的 EXC_BAD_ACCESS 崩溃

ios - 核心剧情: Multiple colors for scatter plot fill area