开发者

How to reuse uitableviewcell??

开发者 https://www.devze.com 2023-03-01 07:01 出处:网络
Every time when scrolling开发者_如何学JAVA the cells reload shouldn\'t be trigged.Apple knows: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableVie

Every time when scrolling开发者_如何学JAVA the cells reload shouldn't be trigged.


Apple knows: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html%23//apple_ref/doc/uid/TP40007451-CH7-SW19


Yes you are correct. I'll elaborate on Steven's post of the Apple documentation a little.

For cells not to be reloaded, first look at this method:

TimeZoneCell *timeZoneCell = (TimeZoneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

It says "give me the existing view that represents a table cell". This is a great saving because if the user is scrolling up or down quickly, creating views can be quite expensive.

Of course, you can't re-use a cell if one hasn't been created in the first place. So, you need to handle that scenario. That's where this code comes in:

if (timeZoneCell == nil) {
    timeZoneCell = [[[TimeZoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    timeZoneCell.frame = CGRectMake(0.0, 0.0, 320.0, ROW_HEIGHT);
}

It says, "is the cell nil?", i.e., have we not yet created a table cell? It then goes on to create one. That cell will subsequently be be re-used.

The actual contents of the cells will change, for example the label or whatever. However you will continue to re-use the existing objects.

0

精彩评论

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