2016-11-13 147 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