2016-11-13 86 views
0

我有一个数据列表,我从中搜索一些项目,然后选择其中的一个扩展选定的单元格。然后,当我清除搜索栏中的搜索文本时,我希望显示原始列表。我能够显示原始列表中的所有项目,但搜索期间选定的单元格未更新。附加快照和代码更好地理解:UITableViewCell不能正确显示文本

Original List

Search for text and select item on row 3

Clear search and display original list.

请注意,在第一形象,列#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]”,并且始终创建新的单元格而不是重用,但没有任何帮助。还有什么可以帮助的?

谢谢!

+0

不知道,但这个问题可能是在您调用[自我displayDataOnTheCell。尝试在cellForRowAtIndexPath中执行此操作,因为即使已经显示单元格,当您调用[self.tableView reloadData]时也会调用该单元格。此外,我会建议做这个[tableView registerNib:[UINib nibWithNibName:@“BranchDetailsTableViewCell”bundle:nil] forCellReuseIdentifier:stateListCellId]在视图控制器内调用...这种方式可以删除if(!stateListCell){/ *。 .. * /}检查cellForRowAtIndexPath。 – ppalancica

+0

@ppalancica - 试过了,没有帮助。我认为有些东西正在缓存单元格。我甚至尝试覆盖单元格上的prepareForReuse方法,并将文本设置为零,但这也不起作用。 –

+0

我可以写一个解决方案的博客文章。也许下周会看到。这并不难,你可能错过了某处的一些小细节...... – ppalancica

回答

1

我能得到它在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]; 
    } 
} 
0

我认为问题在于您为每个单元使用相同的单元标识符。尝试使用不同的小区标识符:

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

    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld",(long)indexPath.row]; 
    BranchDetailsTableViewCell *stateListCell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier]; 

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

    return stateListCell; 
} 
+0

我认为具有相同标识符的全部目的是重复使用单元,而不是每次都创建新的单元。但我也试过,并没有帮助。我觉得有些东西在缓存单元格。我甚至尝试覆盖单元格上的prepareForReuse方法,并将文本设置为零,但这也不起作用。 –

+0

我也很困惑不重复使用单元格。但是我有一个类似的问题,你有什么和创建固定的个人标识符。我的表格包含很多内容,文字,图片和视频,而且我没有遇到任何有关内存或性能的问题。所以我想不要重复使用单元格标识符。 – Alex