0 objective-c reusability uitableview ios4 ios
我有一个包含9个部分和56行的表格.
我想为每个单元格添加文本标签.我创建了一个NSArray menuList56 NSDictionaries和一个包含每个部分(sectionsArray)行数的数组.
这是我的代码,但它根本无法正常工作:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题?
在iOSSimulator,我看到当我滚动时,单元格的文本有时会发生变化,并且标签的顺序不正确.
填写您单元格的所有代码都在:
if (cell == nil) {
Run Code Online (Sandbox Code Playgroud)
声明,所以你最终创建了少量的单元格并填充它们(当cell == nil时),然后只返回出列的单元格.
应该是这样的:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
这就是代码正在做的事情:
Try to get a reusable cell
If no reusable cell is available
{
create a new cell
}
Fill in the values for the cell
因此,当一个单元格滚动屏幕时,它将被放入重用队列中.当一个单元格即将进入屏幕时,您将cellForRowAtIndexPath被调用.分配新单元比重用现有单元要慢,因此首先尝试从重用队列中获取单元,但如果没有可用,则创建一个新单元.然后你填写你的价值观.
| 归档时间: |
|
| 查看次数: |
3508 次 |
| 最近记录: |