2

うまくいかないようですdequeueReusableCellWithIdentifier

ストーリーボードを使用できないようにIOS4のプロジェクトをビルドする必要がありますが、ARCを使用しています。

2つのセクションがあり、それぞれに1つの行があるとします。

以下のコードを見ると、ARCが「自動解放」コードを挿入するため、所有権を渡すために強力なプロパティを使用しています。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *cellIdentifier = @"TableCellIdentifier";

     MainTableCell *cell = (MainTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

     if (cell == nil) 
     {
          self.retainedCell = [[MainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
     }

     [self configureCell:cell atIndexPath:indexPath];

     return cell;
}

ただし、各行について、関数が呼び出されるたびにセルは常にnilになります(したがって、新しいMainTableCellが割り当てられます)。セルが再利用されることはありません。

これは、プログラムでtableView:cellForRowAtIndexPath:を呼び出すことを除いて、それほど問題にはなりません。つまり、既存のセルではなく、毎回新しく割り当てられたセルを取得します。

私が見ることができる唯一の方法は、セルをNSMutableArrayに追加することです。

今足りないものはありdequeueReusableCellWithIdentifierますか?

ありがとう!

編集 私はセルを取得するために以下のコードを使用しています。前述のように、すでに作成されて保持されているはずのセルを再利用せずに、新しいセルを作成しています。すべての行に対してreloadDataを呼び出す必要はなく、特定の行を変更するだけです。

MainTableCell *cell = (MainTableCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[self configureCell:cell atIndexPath:indexPath];
4

1 に答える 1

3

たまたまMainTableCellのキューを解除してから、それがnilであるかどうかの確認に進みます。その時点で、まったく異なるvarを使用してテーブルセルを割り当てます。一体何?これを試して:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"TableCellIdentifier";
    MainTableCell *cell = (MainTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) 
    {
        cell = [[MainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}
于 2012-05-18T05:18:54.770 に答える