2016-11-13 7 views
0

私はいくつかのアイテムを検索したデータのリストを持っており、選択したセルを展開するデータのリストを1つ選択します。その後、検索バーの検索テキストをクリアすると、元のリストが表示されます。検索中に選択されたセルが更新されないことを除いて、元のリストのすべてのアイテムを表示できます。第三の画像内の同じ行で使用するのに対し、第1の画像内の行#3は、テキスト「ABロード」を有することUITableViewCellが正しくテキストを表示しない

Original List

Search for text and select item on row 3

Clear search and display original list.

注意:スナップショットとより良い理解のためにコードを取付けます2番目のイメージと同じセル( "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]呼び出すという問題であってもよいです。セルが既に表示されていても、[self.tableView reloadData]を呼び出すときに呼び出されるので、cellForRowAtIndexPathの内部でそれを実行してください。また、私はビューコントローラ内でこの[tableView registerNib:[UINib nibWithNibName:@ "BranchDetailsTableViewCell" bundle:nil forCellReuseIdentifier:stateListCellId]コールを行うことをお勧めします。if(!stateListCell){/ * .. * /} cellForRowAtIndexPathをチェックします。 – ppalancica

+0

@ppalancica - 試してみましたが、助けになりませんでした。私は何かが細胞をキャッシングしていると思う。私は、セルのprepareForReuseメソッドをオーバーライドしてテキストをnilに設定しようとしましたが、それもうまくいきませんでした。 –

+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メソッドをオーバーライドしてテキストをnilに設定しようとしましたが、それもうまくいきませんでした。 –

+0

私はまた、細胞を再利用しないことについて混乱しました。しかし私は、あなたが持っているものと同様の問題を抱えていて、個別の識別子を作成していました。私のテーブルにはたくさんのコンテンツ、テキスト、画像、ビデオが含まれていますが、私はメモリやパフォーマンスに問題はありませんでした。だから私は、セル識別子を再利用しても問題ないと思います。 – Alex

関連する問題