UITutView的UITableViewCells在滚动时重叠 [英] UITableViewCells with UIButton overlaps while scrolling

查看:142
本文介绍了UITutView的UITableViewCells在滚动时重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个包含一些按钮作为其元素的表视图,视图可以很好地加载,但是向下滚动按钮会重叠。我认为旧的按钮没有被清除,新的按钮放在它上面。请帮我解决这个问题。在此先感谢。

I need to create a table view with some buttons as its elements, view gets loaded fine, but while scrolling down buttons gets overlapped. I think old buttons are not cleared and new buttons are placing over it. Please help me to fix this problem. Thanks in advance.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath           (NSIndexPath *)indexPath
{
NSLog(@"index path %ld",(long)indexPath.row);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}


    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 160.0, 25);
    [cell.contentView addSubview:button];


return cell;
}


推荐答案

只需更换行

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]

现在

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

  (or) //use this in cell for rowatindexpath

for(UIView *view in cell.contentView.subviews){
    if ([view isKindOfClass:[UIView class]]) {
        [view removeFromSuperview];
    }
}

这篇关于UITutView的UITableViewCells在滚动时重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆