Iphone UITableView将单元格重叠在一起
我认为我的表格很好,直到我改变了背景,使之有点透明,在新的出列单元格下面显示了以前的单元格。这是我的Iphone UITableView将单元格重叠在一起,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我认为我的表格很好,直到我改变了背景,使之有点透明,在新的出列单元格下面显示了以前的单元格。这是我的tableView:cellforrowatinexpath:方法中的代码。我取出了冗余标签设置,它们与timeLabel完全相同 static NSString* cellIdentifer = @"Cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifer]; if (
tableView:cellforrowatinexpath:
方法中的代码。我取出了冗余标签设置,它们与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];
您正在创建新的
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"];
我建议您创建一个而不是一个凌乱的东西,主要问题是cell=
[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]代码>方法,所以如果您不想更改任何内容,请使用如下方法UITableViewCell*cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]代码>
这不是更好的方法,但你可以使用它。因为在这种情况下,您的单元将不会被重用。一个更好的解决方案是,根据您的选择定制电池并使用它。电池的高度是多少?120。在XIB和表格视图中设置:heightForRowAtIndexPath:
调用是否可以附加屏幕图像shot@Chris我把你的代码放在我的方法中,它给了我良好的表现。您确定要从表格视图:heightForRowAtIndexPath:
重新调谐120吗?@BestCoder我附加了一个屏幕截图。。对不起,它太大了。是的,为了简单起见,你必须创建自定义单元格。另一方面,随着子视图的增长,它会变得更加复杂,可读性也会降低。