我有一个数据列表,我从中搜索一些项目,然后选择其中的一个扩展选定的单元格。然后,当我清除搜索栏中的搜索文本时,我希望显示原始列表。我能够显示原始列表中的所有项目,但搜索期间选定的单元格未更新。附加快照和代码更好地理解:UITableViewCell不能正确显示文本
请注意,在第一形象,列#3具有文本“AB道”,而在同一行中第三图像使用与第二张图像相同的单元格(它应该更新为“AB 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];
}
}
看起来它只是是因为当我调试,我确实看到了“AB道”在数组中没有得到再次渲染单元,它是不是显示它的细胞。我尝试调用“[cell setNeedsDisplay]”,并且始终创建新的单元格而不是重用,但没有任何帮助。还有什么可以帮助的?
谢谢!
不知道,但这个问题可能是在您调用[自我displayDataOnTheCell。尝试在cellForRowAtIndexPath中执行此操作,因为即使已经显示单元格,当您调用[self.tableView reloadData]时也会调用该单元格。此外,我会建议做这个[tableView registerNib:[UINib nibWithNibName:@“BranchDetailsTableViewCell”bundle:nil] forCellReuseIdentifier:stateListCellId]在视图控制器内调用...这种方式可以删除if(!stateListCell){/ *。 .. * /}检查cellForRowAtIndexPath。 – ppalancica
@ppalancica - 试过了,没有帮助。我认为有些东西正在缓存单元格。我甚至尝试覆盖单元格上的prepareForReuse方法,并将文本设置为零,但这也不起作用。 –
我可以写一个解决方案的博客文章。也许下周会看到。这并不难,你可能错过了某处的一些小细节...... – ppalancica