我正在尝试使用两种不同类型的 UITableViewCells 打印 UITableView。
- 空表格 View 单元格,不包含任何内容(前 2 个单元格),只有黑色背景
- 包含标签、图像等的表格 View 单元格,从 1 开始(第 3 个和后续单元格)
我的表格 View 的高度在任何时候都能够容纳至少 3 个表格 View 单元格。
当我第一次加载表格 View 时,表格 View 看起来非常好 - 第一个两行是黑色的,然后是带有“1”标签的第三行。
但是,向下滚动到底部然后向上滚动。我的前两个空单元格(应该是空的)填充了只能在第三个和后续单元格中找到的东西。
我怀疑这种行为来自重用单元格的 TableView ,但我仍然无法弄清楚。
代码片段:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch ([indexPath row]) {
case 0:
cell.contentView.backgroundColor = [UIColor blackColor];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
break;
case 1:
cell.contentView.backgroundColor = [UIColor blackColor];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
break;
default:
cell.contentView.backgroundColor = [UIColor blackColor];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *rowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 30, 200, 30)];
rowLabel.text = [NSString stringWithFormat:@"Row %d", [indexPath row]-1];
rowLabel.backgroundColor = [UIColor blackColor];
rowLabel.textColor = [UIColor whiteColor];
[rowLabel setFont:[UIFont fontWithName:@"GillSans-Bold" size:18]];
[cell.contentView rowLabel]; [rowLabel release];
UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 40, 40)];
[aButton setImage:anImage forState:UIControlStateNormal];
[cell.contentView addSubview:aButton];
[aButton release];
break;
}
return cell;
}
最佳答案
您的出列单元格仍将包含您添加的所有按钮和标签,您所做的只是在单元格出列时设置背景颜色。
您有几个选择:
- 为黑色区域使用
tableHeaderView
- 使用带部分的分组 TableView
- 对第 0 行和第 1 行的单元格使用不同的重用标识符。
对于后一种选择,您的cellForRowAtIndexPath
应该是:
static NSString *CellIdentifier = @"Cell";
static NSString *blankCellIndentifier = @"BlankCell";
if (indexPath.row == 0 || indexPath.row == 1)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:blankCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:blankCellIdentifier] autorelease];
cell.contentView.backgroundColor = [UIColor blackColor];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return cell;
}
else
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *rowLabel;
UIButton *aButton;
if (cell == nil)
{
// Here, you create all of the new objects
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.contentView.backgroundColor = [UIColor blackColor];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
rowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 30, 200, 30)];
rowLabel.backgroundColor = [UIColor blackColor];
rowLabel.textColor = [UIColor whiteColor];
[rowLabel setFont:[UIFont fontWithName:@"GillSans-Bold" size:18]];
[cell.contentView addSubview:rowLabel];
rowLabel.tag = 1;
[rowLabel release];
aButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 40, 40)];
[aButton setImage:anImage forState:UIControlStateNormal];
[cell.contentView addSubview:aButton];
aButton.tag = 2;
[aButton release];
}
// Here, you just configure the objects as appropriate for the row
rowLabel = (UILabel*)[cell.contentView viewWithTag:1];
rowLabel.text = [NSString stringWithFormat:@"Row %d", [indexPath row]-1];
return cell;
}
关于iphone - UITableView 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7376774/