再利用可能cellを使用する2つの方法
7515 ワード
1.最も一般的な方法
2.登録使用
この方法ではtableViewを作成するときに登録する必要があります
先に登録しないと、プログラムは実行時にcrashします.
unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
cellとして識別されたセルをキューに入れることはできません.この識別子にnibまたはクラスを登録するか、storyboardにあるプロトタイプセルを接続する必要があります.
私から見れば、最初の方法を使うのはもっと柔軟で便利です.
1)第2の方法はiOS 6以降でサポートを開始し、初期のバージョンではサポートされておらず、本体に適用すると互換性が大きな問題となっている.
2)第2の方法はコードがより簡素化されているが,cell上のtextLabelだけを変更しても要求を満たすことができない場合,開発者は自分のニーズに応じて異なるviewを追加する必要がある.この場合,どのcellが再利用されているのか,そうでないのかを特定できないため,1つのcellを追加するにはviewを追加する必要があります.このように、携帯電話の画面をスライドするとき、それらの再利用されたcellに再び同じviewを追加します.スライド回数が増えるにつれて、1つのcellに重ねられたviewも多くなり、大量のメモリ消費をもたらします.これはcell再利用の考えとは逆です.
3)第2の方法は使用する前にtableViewに登録してから使用しなければならない.そうしないとプログラムがクラッシュするが、開発中に忘れがちで、cellの表示スタイルを設定することはできない
1つ目の方法を使うときは、 cellにviewを追加し、同じ部分をif文で完成させると、同じcellに同じviewをたくさん重ねないようになります
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if ( !cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
return cell;
}
2.登録使用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}
この方法ではtableViewを作成するときに登録する必要があります
// tableView , cell UITableViewCell
// cell , cell , ;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
先に登録しないと、プログラムは実行時にcrashします.
unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
cellとして識別されたセルをキューに入れることはできません.この識別子にnibまたはクラスを登録するか、storyboardにあるプロトタイプセルを接続する必要があります.
私から見れば、最初の方法を使うのはもっと柔軟で便利です.
1)第2の方法はiOS 6以降でサポートを開始し、初期のバージョンではサポートされておらず、本体に適用すると互換性が大きな問題となっている.
2)第2の方法はコードがより簡素化されているが,cell上のtextLabelだけを変更しても要求を満たすことができない場合,開発者は自分のニーズに応じて異なるviewを追加する必要がある.この場合,どのcellが再利用されているのか,そうでないのかを特定できないため,1つのcellを追加するにはviewを追加する必要があります.このように、携帯電話の画面をスライドするとき、それらの再利用されたcellに再び同じviewを追加します.スライド回数が増えるにつれて、1つのcellに重ねられたviewも多くなり、大量のメモリ消費をもたらします.これはcell再利用の考えとは逆です.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
imageView.frame = CGRectMake(15, 2, 40, 40);
[cell addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(65, 2, 100, 20)];
label.text = @"Hello";
[cell addSubview:label];
UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 22, 100, 20)];
subLabel.text = @" ";
[cell addSubview:subLabel];
return cell;
} // , , cell , label ; , , ,
3)第2の方法は使用する前にtableViewに登録してから使用しなければならない.そうしないとプログラムがクラッシュするが、開発中に忘れがちで、cellの表示スタイルを設定することはできない
1つ目の方法を使うときは、 cellにviewを追加し、同じ部分をif文で完成させると、同じcellに同じviewをたくさん重ねないようになります
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if ( !cell )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; // cell subTitle
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
imageView.frame = CGRectMake(15, 2, 40, 40);
[cell addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(65, 2, 100, 20)];
label.text = @"Hello";
[cell addSubview:label];
UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 22, 100, 20)];
subLabel.text = @" ";
[cell addSubview:subLabel];
}
return cell;
}