私はいくつかのアイテムを検索したデータのリストを持っており、選択したセルを展開するデータのリストを1つ選択します。その後、検索バーの検索テキストをクリアすると、元のリストが表示されます。検索中に選択されたセルが更新されないことを除いて、元のリストのすべてのアイテムを表示できます。第三の画像内の同じ行で使用するのに対し、第1の画像内の行#3は、テキスト「ABロード」を有することUITableViewCellが正しくテキストを表示しない
注意:スナップショットとより良い理解のためにコードを取付けます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]"と呼んでみました。また、再使用する代わりに常に新しいセルを作成しましたが、何も助けてくれませんでした。他に何が助けてくれるの?
ありがとうございます!
わからないが、あなたは[自己displayDataOnTheCell]呼び出すという問題であってもよいです。セルが既に表示されていても、[self.tableView reloadData]を呼び出すときに呼び出されるので、cellForRowAtIndexPathの内部でそれを実行してください。また、私はビューコントローラ内でこの[tableView registerNib:[UINib nibWithNibName:@ "BranchDetailsTableViewCell" bundle:nil forCellReuseIdentifier:stateListCellId]コールを行うことをお勧めします。if(!stateListCell){/ * .. * /} cellForRowAtIndexPathをチェックします。 – ppalancica
@ppalancica - 試してみましたが、助けになりませんでした。私は何かが細胞をキャッシングしていると思う。私は、セルのprepareForReuseメソッドをオーバーライドしてテキストをnilに設定しようとしましたが、それもうまくいきませんでした。 –
私は解決策のためのブログ記事を書くかもしれません。たぶん来週、見えるだろう。それほど難しいことではないかもしれませんが、おそらくどこか小さなディテールを見逃してしまいました... – ppalancica