我有一個數據列表,我從中搜索一些項目,然後選擇其中的一個擴展選定的單元格。然後,當我清除搜索欄中的搜索文本時,我希望顯示原始列表。我能夠顯示原始列表中的所有項目,但搜索期間選定的單元格未更新。附加快照和代碼更好地理解: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