自定义单元格内容在UITableView中重叠

时间:2015-10-14 06:39:29

标签: ios uitabview

我知道这个问题已被多次询问,但我的问题有所不同。

我在单元格的内容视图中以编程方式创建UIView和UIImageView。当TableView第一次出现时看起来很完美,但是当我向下和向上滚动时,这似乎是重叠的。

没有滚动的屏幕截图:

enter image description here

滚动后的屏幕截图:

enter image description here

我遵循的代码:

 viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1];
[cell.contentView addSubview:viewForHead];

UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60 )];
imageViewForDP.image = [UIImage imageNamed:@"dog_1.png"];
//[cell.viewForContents addSubview:imageViewForDP];
imageViewForDP.layer.cornerRadius = 30;
imageViewForDP.clipsToBounds = YES;
[viewForHead addSubview:imageViewForDP];

请让我解决这个问题。感谢

3 个答案:

答案 0 :(得分:2)

将此用于- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

if ([cell.contentView subviews]){
    for (UIView *subview in [cell.contentView subviews]) {
        [subview removeFromSuperview];
    }
}

答案 1 :(得分:1)

每次单元格出列时,您都会将viewForHead添加为子视图。所以你将它们添加到彼此之上。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CELL"] autorelease];

//      This is where you CREATE your cell contents.
        viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)];
        viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1];
        [cell.contentView addSubview:viewForHead];

         UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60 )];
         imageViewForDP.image = [UIImage imageNamed:@"dog_1.png"];
//       [cell.viewForContents addSubview:imageViewForDP];
         imageViewForDP.layer.cornerRadius = 30;
         imageViewForDP.clipsToBounds = YES;
         imageView.tag = 1
         [viewForHead addSubview:imageViewForDP];

    }

//     this is where you UPDATE your viewForHead image and any other elements inside your cell
       UIImageView *imageView = [cell.contentView viewWithTag:1];
       imageView.image = // your new image

    return cell;

}

对UITableViewCell进行子类化并使用xib构建布局会更好,然后您可以直接访问单元属性。一个更清洁的解决方案。

MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier@"CELL"];
if (cell == nil) {
    cell = [[MyCustomCell alloc] init]; // you ID is set in interface builder
}  

cell.imageView.image = // your new image here.
cell.someLabel.text = @"some new text here"

答案 2 :(得分:0)

这个问题是因为表视图单元格被重用,而你又在单元格上添加了一个视图。

尝试以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] ;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

UIView *viewForHead = (UIView *)[cell.contentView  viewWithTag:1];
if (viewForHead==nil) {

    viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 20)];
    viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:0.5];
    viewForHead.tag = 1;
    [cell.contentView addSubview:viewForHead];
}
return cell;}