2

誰かが違いを説明できますか

static NSString* CellIdentifier = @"Cell";

NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%i", indexPath.row];

最初のものをいつ使用し、2番目のものをどこで使用する必要がありますか?

4

1 に答える 1

4
static NSString* CellIdentifier = @"Cell";

この識別子 (他に何もないと仮定) は、新しいセルが必要なときにすべての行がプルされるセルのプールを識別します。

NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%i", indexPath.row];

この識別子は、行ごとにセルのプールを作成します。つまり、行ごとにサイズ 1 のプールを作成し、その 1 つのセルは常にその行だけに使用されます。

通常、常に最初の例を使用する必要があります。次のような 2 番目の例のバリエーション:

NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%i", indexPath.row % 2];

他のすべての行に特定の背景色またはそのようなものを持たせたい場合に役立ちます。

ここからセル作成を適切にセットアップする方法の例:

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

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"mainTitleKey"];
    cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"];
    NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"];
    UIImage *theImage = [UIImage imageWithContentsOfFile:path];
    cell.imageView.image = theImage;

    return cell;
}
于 2012-04-04T17:48:37.990 に答える