UITableView将单元格叠加在一起

Chr*_*ris 4 iphone objective-c uitableview

我认为我的桌子很好,直到我改变背景有点透明,这显示了新出列的细胞下面的先前细胞.这是我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];
Run Code Online (Sandbox Code Playgroud)

这是正在发生的事情的屏幕截图

Ind*_*ore 5

您正在创建新的UIView,UILabel即使重复使用单元格,结果显示新单元格下方的旧单元格.

这样做

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"];
Run Code Online (Sandbox Code Playgroud)

我建议你创建一个自定义,而不是有这个混乱的东西UITableViewCell