要約すると、私は 2 つの問題に直面しています。1 つ目は、時々、いくつかのセルがテーブルビューから消えて、ランダムに再表示されることです。2 つ目は、iPhone 4 以下でのスクロール パフォーマンスの問題です。
第1の問題:
[cell contentView] のサブビューを追加して、UITableView に要素の単純なリストを表示しています。ほとんどの場合はうまく機能しますが、スクロールすると(そして本当にランダムに)セルが消えることがあります。
この「消えた」セルは再利用されるため、スクロールすると再利用されたセルも「空白」になります。また、スクロール中にセルがランダムに再表示/非表示になることがあります。
cellForRowAtIndexPath: メソッドのコードは次のとおりです。
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
LBSelectorChampionViewController *item = [self itemAtIndexPath:indexPath];
if (cell == nil) {
NSLog(@"CELL NIL");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[[item view] setTag:99];
[[item champName] setTag:50];
[[item champImage] setTag:51];
[[item buttonFavorite] setTag:52];
[[cell contentView] addSubview:[item view]];
}
UILabel *championLabel = (UILabel*)[[[cell contentView] viewWithTag:99] viewWithTag:50];
LBFavoriteChampionButton *buttonFavorite = (LBFavoriteChampionButton*)[[[cell contentView] viewWithTag:99] viewWithTag:52];
UIImageView *championImage = (UIImageView*)[[[cell contentView] viewWithTag:99] viewWithTag:51];
// Text
[championLabel setText:[item displayValue]];
[championImage setImageWithURL:[NSURL URLWithString:[item imageUrl]]];
// Fav button
[buttonFavorite setCurrentChampion:item];
if ([[self favoritesChampions] containsObject:item]) {
[buttonFavorite setSelected:YES];
[buttonFavorite setIsFavorite:YES];
} else {
[buttonFavorite setSelected:NO];
[buttonFavorite setIsFavorite:NO];
}
return cell;}
}
すべてをログに記録しようとしましたが、「アイテム」が時々 null になる可能性があるかどうかを確認しましたが、決してそうではありません。しかし、セルが消えると、[[[cell contentView] subviews] count] は 0 に等しくなり、通常は 1 になるはずです。ここで何が起こっているのか本当にわかりません。実際のデバイスとシミュレーターでテストしたところ、両方で発生しました。
2番目の問題:
私の他の「問題」は、テーブルビューのスクロールがiPhone 4で期待するほどスムーズではないことです(iOS 6およびiOS 7でテスト、同じ結果:-( )。SDWebImageを使用してロードしています画像を非同期的にキャッシュして、セルを再利用していますが、パフォーマンスを向上させるにはどうすればよいでしょうか? iPhone 4S でテストしましたが、問題はありませんでした。スクロールは非常にスムーズです。
私は何か間違っていますか?私の問題の1つについてのアイデアはありますか?
私に答えてくれてありがとう。
編集:問題を解決するための最初の試み
カスタム UITableViewCell サブクラスでセルをカスタマイズしようとしています:
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"LBListChampionCell";
LBListChampionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LBListChampionCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
LBSelectorChampionViewController *item = [self itemAtIndexPath:indexPath];
[[cell championName] setText:[item displayValue]];
[[cell championLogo] setImageWithURL:[NSURL URLWithString:[item imageUrl]]];
return cell;
}
「消える」バグは今のところ再現できませんでしたが(再現するのに少し時間がかかる場合もあります...)、パフォーマンスに関してはまったく改善されていません:(。ダミーテキストを設定しているだけでも: [[cell ChampionName] setText:@"Test"]; (および項目部分にコメントを付ける) スクロールはまだスムーズではありません。
何か案は ?
EDIT 2:最良の解決策(rdelmarに感謝)
UITableViewCell のサブクラスを作成し、 nib を viewDidLoad にロードします。
- (void)viewDidLoad
{
...
UINib *nib = [UINib nibWithNibName:@"LBListChampionCell" bundle:nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:@"LBListChampionCell"];
}
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
LBListChampionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LBListChampionCell"];
LBSelectorChampionViewController *item = [self itemAtIndexPath:indexPath];
[[cell championName] setText:[item displayValue]];
[[cell championLogo] setImageWithURL:[NSURL URLWithString:[item imageUrl]]];
...
}
スクロールのパフォーマンスが向上しました (iPhone 4 ではまだ完全にスムーズではありませんが、うまく機能します)。さらに、もう細胞が消えていないようです:-)
ありがとう、ルデルマー!