重用细胞...... :( [英] Reuse cell... :(
问题描述
我有一个包含9个部分和56行的表格。
I have a table with 9 sections and 56 rows.
我想为每个单元格添加文本标签。我用56 NSDictionaries
和一个数组创建了一个 NSArray
menuList
包含每个部分中的行数( sectionsArray
)。
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;
}
此代码有什么问题?
在 iOSSimulator
中,我看到当我滚动时,单元格的文本有时发生变化,标签的顺序不正确。
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) {
语句,所以你最终创建了一个少量细胞并将它们填入(当细胞== 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.
这应该是:
// 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
因此,当一个单元格滚动屏幕时,它会被放入进入重用队列。当一个单元格即将进入屏幕时,将调用 cellForRowAtIndexPath
。分配新单元格比重用现有单元格要慢,因此首先尝试从重用队列中获取单元格,但如果没有可用,则创建一个新单元格。然后你填写你的价值观。
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.
这篇关于重用细胞...... :(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!