0

私は現在次のようにデキューしている3つのカスタム UITableViewCells を持つ UITableView を持っています:

    if (indexPath.row == 0) {
         static NSString *CellIdentifier = @"MyCell1";
         MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
             cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
         }
         return cell;
    }
    if (indexPath.row == 1) {
         static NSString *CellIdentifier = @"MyCell2";
         MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
             cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
         }
         return cell;
    }
    if (indexPath.row == 2) {
         static NSString *CellIdentifier = @"MyCell3";
         MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
             cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
         }
         return cell;
    }

これを複数の方法で実行しようとしましたが、問題は、tableView をスクロールすると、最初のセルが 3 番目のセルの場所に表示され、その逆の場合もあります。奇妙なキャッシングが行われているようです。

誰かが理由を知っていますか?ありがとう。

4

1 に答える 1

1

常に同じセル クラスを割り当てているため、投稿したコードには意味がありません。セル識別子は、特定のセルを識別するために使用されるのではなく、使用しているサブクラスを識別するために使用されます。

したがって、コードを次のように変更します。

static NSString *CellIdentifier = @"MyCell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
     cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;

indexPath.section と indexPath.row に基づいて、willDisplayCell でセルの内容を適切に設定します。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
于 2012-06-09T02:15:45.983 に答える