Reuse cell... :(
I have a table with 9 sections and 56 rows.
I want to add a text label for each cell. I created a NSArray
menuList
with 56 NSDictionaries
and an array containing the number of rows in each section (sectionsArray
).
Here is my code, but it doesn't work properly at all:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Use existing cell (reusable)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
//If no existing cell,开发者_开发问答 create a new one
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
//Define cell accessory type
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//Create a subView for the cell
CGRect subViewFrame = cell.contentView.frame;
subViewFrame.origin.x += kInset;
subViewFrame.size.width = kInset + kSelectLabelWidth;
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:subViewFrame];
//SubView design
selectedLabel.textColor = [UIColor blackColor];
selectedLabel.highlightedTextColor = [UIColor whiteColor];
selectedLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:selectedLabel];
int indRow = 0;
for (int i =0; i < indexPath.section; i++) {
indRow += [[sectionsArray objectAtIndex:i] intValue];
}
indRow += indexPath.row;
NSDictionary *cellText = [menuList objectAtIndex:indRow];
selectedLabel.text = [cellText objectForKey:@"selection"];
[selectedLabel release];
}
return cell;
}
What's wrong in this code?
In iOSSimulator
, i see that cell's text change sometimes when I'm scrolling, and labels are not in the right order.
All the code that fills in your cells is with in the:
if (cell == nil) {
statement, so you end up creating a small number of cells and filling them in (when cell==nil) and then just return the dequeued ones.
This is what it should be:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Use existing cell (reusable)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
//If no existing cell, create a new one
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
//Define cell accessory type
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//Create a subView for the cell
CGRect subViewFrame = cell.contentView.frame;
subViewFrame.origin.x += kInset;
subViewFrame.size.width = kInset + kSelectLabelWidth;
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:subViewFrame];
//SubView design
selectedLabel.textColor = [UIColor blackColor];
selectedLabel.highlightedTextColor = [UIColor whiteColor];
selectedLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:selectedLabel];
}
// At this point cell whether it is a reused cell or a new one
// cell points to the object we need to fill in.
int indRow = 0;
for (int i =0; i < indexPath.section; i++) {
indRow += [[sectionsArray objectAtIndex:i] intValue];
}
indRow += indexPath.row;
NSDictionary *cellText = [menuList objectAtIndex:indRow];
selectedLabel.text = [cellText objectForKey:@"selection"];
[selectedLabel release];
return cell;
}
This is what the code is doing:
Try to get a reusable cell If no reusable cell is available { create a new cell } Fill in the values for the cell
So when a cell scrolls of the screen it is put into a reuse queue. When a cell is about to come onto the screen your cellForRowAtIndexPath
is called. Allocating a new cell is slower than reusing an existing one, so you first try to get a cell from the reuse queue, but if one is not available you create a new one. Then you fill in your values.
精彩评论