开发者

UITableViewCell, dequeue and autorelease?

I am pretty sure that what I am doing here is right, but I just wanted to check 开发者_如何学运维that the [cell autorelease] is not freeing my cells too soon and that dequeueReusableCellWithIdentifier has queued cells around waiting for reuse. My understanding is that the queue is a separate entity in as much as its storing a blueprint for the cell (a bit like a nib) and not the actual object?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LOC_ID"];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LOC_ID"];
        [cell autorelease];
    }
    NSUInteger row = [indexPath row];
    //id thisLocation = [locationList objectAtIndex:row];
    [[cell textLabel] setText:[NSString stringWithFormat:@"R_%u", row]];
    return cell;
}

EDIT: To clarify I am interested in the what happens with regard to lifespan and the cells. I understand that I alloc them and then autorelease them, does the table then take ownership of the cells after they are returned from cellForRowAtIndexPath? so they ten belong to the UITableView, or am I getting confused?


No it is actually storing the cell object per identifier.

When the table first gets created the cell is created from scratch. So when you scroll the tableview, the new cells are dequeued based on the identifier. For performance reasons, it can be reused by for example, setting text on the cell; so there is no need to create a new cell from scratch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜