UITableView:单元格重叠在彼此上方。

4

我原本认为我的表格已经很好了,直到我把背景色设置成稍微透明一点,才发现之前的单元格在新出列的单元格下方仍然可见。这是我tableView:cellForRowAtIndexPath:方法中的代码。我去掉了冗余的标签设置,它们与timeLabel完全相同。

static NSString* cellIdentifer = @"Cell";

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifer];

if (!cell)
{
    cell= [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellIdentifer];
}

int offset = 5;
int rowHeight = 120;
UIView* cellView = [[UIView alloc] initWithFrame: CGRectMake(offset, offset, 320 - offset * 2, rowHeight - offset * 2)];

cellView.backgroundColor = indexPath.row % 2 == 0 ? [UIColor colorWithWhite: 1.0 alpha: .8 ] : [UIColor colorWithWhite: .3 alpha: .8];

int padding = 5;

int x1 = 5;
int x2 = CGRectGetWidth(cellView.frame) / 3;
int x3 = x2 * 2;

int y1 = 5;
int y2 = CGRectGetHeight(cellView.frame) / 2 + padding;

int width = CGRectGetWidth(cellView.frame) / 3 - padding;
int sHeight = CGRectGetHeight(cellView.frame) / 2 - padding;
int bHeight = CGRectGetHeight(cellView.frame) - padding;

CGRect cell1 = CGRectMake(x1, y1, width, sHeight);

int row = indexPath.row;// - 1;

UILabel* timeLabel = [[UILabel alloc] initWithFrame: cell1];
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.text = [[self.tableData objectAtIndex: row] objectForKey: @"time"];
[cellView addSubview: timeLabel];


[cell.contentView addSubview: cellView];

Here's a screenshot of what's happening


你的单元格高度是多少? - Inder Kumar Rathore
  1. 在XIB中设置tableView:heightForRowAtIndexPath:方法
- Chris
1
你能附上你的屏幕截图图片吗? - aks.knit1108
@InderKumarRathore 是的,我从那个方法返回120。 - Chris
不要使用deque方法,也不要每次制作单元格时都不使用任何标识符。因为我看到你的单元格重叠了。 - aks.knit1108
显示剩余3条评论
2个回答

5
您正在创建新的UIViewUILabel,即使单元格被重用,这会导致在您的新单元格下面显示旧单元格。
请按照以下方式操作:
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifer];

if (!cell)
{
    cell= [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellIdentifer];

    int offset = 5;
    int rowHeight = 120;
    UIView* cellView = [[UIView alloc] initWithFrame: CGRectMake(offset, offset, 320 - offset * 2, rowHeight - offset * 2)];
    cellView.tag = 200;
    cellView.backgroundColor = indexPath.row % 2 == 0 ? [UIColor colorWithWhite: 1.0 alpha: .8 ] : [UIColor colorWithWhite: .3 alpha: .8];

    int padding = 5;

    int x1 = 5;
    int x2 = CGRectGetWidth(cellView.frame) / 3;
    int x3 = x2 * 2;

    int y1 = 5;
    int y2 = CGRectGetHeight(cellView.frame) / 2 + padding;

    int width = CGRectGetWidth(cellView.frame) / 3 - padding;
    int sHeight = CGRectGetHeight(cellView.frame) / 2 - padding;
    int bHeight = CGRectGetHeight(cellView.frame) - padding;

    CGRect cell1 = CGRectMake(x1, y1, width, sHeight);

    int row = indexPath.row;// - 1;
    UILabel* timeLabel = [[UILabel alloc] initWithFrame: cell1];
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.tag = 300;
    [cellView addSubview: timeLabel];
    [cell.contentView addSubview: cellView];
}

UIView *view = [cell.contentView viewWithTag:200];
UILabel *timeLabel = (UILabel*)[view viewWithTag:300];
timeLabel.text = [[self.tableData objectAtIndex: row] objectForKey: @"time"];

建议你创建一个自定义的UITableViewCell而不是使用这个混乱的东西。


是的,为了简单起见,您必须创建自定义单元格。否则,随着子视图的增加,它将变得更加复杂和难以阅读。 - Inder Kumar Rathore

0
主要问题在于cell= [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellIdentifer];方法,如果您不想更改任何内容,则可以使用以下方法:UITableViewCell *cell= [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: nil]; 这不是更好的方法,但您可以使用它。因为在这种情况下,您的单元格将不会被重用。更好的解决方案是使用自定义单元格并进行选择。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接