【发布时间】:2011-03-30 15:00:22
【问题描述】:
当我向上和向下滚动时,我的 UITableView 会在彼此之上添加重复的标签。所以最终添加了这么多标签,以至于添加速度变慢并崩溃。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *label;
label = [[UILabel alloc] initWithFrame:nameFrame];
[label setText:[name objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[cell addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:statusFrame];
[label setText:[status objectAtIndex:indexPath.row + indexPath.section]];
[label setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:(UITextAlignmentRight)];
[cell addSubview:label];
[label release];
return cell;
}
【问题讨论】:
-
与重复标签无关,但您确定要为数组索引执行
indexPath.row + indexPath.section吗?例如,这将为第 0 节 + 第 1 行返回与第 1 节 + 第 0 行相同的索引。
标签: iphone objective-c uitableview duplicates uilabel