我正在尝试使用两种不同类型的UITableViewCells打印UITableView。
我的表格视图的高度在任何时候都能够容纳至少3个表格视图单元格。
当我第一次加载表格视图时,表格视图看起来非常精细 - 前两行是黑色,后面是第三行,标签上写着“1”。
但是,向下滚动到底部然后向上滚动。我的前两个空单元格(应该是空的)充满了只能在第3个和后续单元格中找到的东西。
我怀疑这种行为来自重复使用单元格的表格视图,但我仍然无法弄明白。
代码段:
- (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;
}
答案 0 :(得分:1)
您出列的单元格仍将包含您添加的所有按钮和标签,您所做的只是在单元格出列时设置背景颜色。
您有几个选择:
tableHeaderView
为您的黑色区域对于后一种选择,您的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;
}
答案 1 :(得分:0)
对于每种不同类型的单元格,您需要不同的重用标识符。所以,你的情况你需要三个reuseIdentifier's。