1

かなり基本的なcellForRowAtIndexPathの実装。

なぜアナライザーがここでメモリリークしていると言っているのか疑問に思います。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    MyObject *object = [[self.datasource objectForKey:key] objectAtIndex:indexPath.row];

    cell.textLabel.text = object.title;

    return cell;
}

アナライザーは、「セル」が潜在的にリークしていることを教えてくれます。これはアナライザーの単なる弱点ですか、それともこれをどのように処理する必要がありますか?

注:次のように自動リリースの呼び出しを追加します。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] autorelease];

クラッシュを引き起こします。何かご意見は?

4

2 に答える 2

1

ARCを使用していない場合は、セルを自動解放する必要があります。

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

これは、から取得した後に自動解放することと同じではないことに注意してください。これはdequeueReusableCellWithIdentifier:、そのメソッドが特定のセルに対して複数回呼び出され、過剰な自動解放によってクラッシュが発生するためです。

于 2012-11-20T17:39:55.693 に答える
0

新しいセルを割り当てる行に自動リリースを配置する必要があります。

 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
于 2012-11-20T17:41:51.130 に答える