Ios 带UIButton的UITableViewCells在滚动时重叠

Ios 带UIButton的UITableViewCells在滚动时重叠,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我需要创建一个表视图,其中包含一些按钮作为其元素,视图可以很好地加载,但向下滚动时,按钮会重叠。我认为旧的按钮没有被清除,新的按钮被放置在上面。请帮我解决这个问题。提前谢谢 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath { NSLog(@"index path %ld",(long)indexPath.row); st

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

- (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];
    }
}
只需更换线路

  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];
    }
}

请在添加按钮作为子视图之前插入以下代码,在添加新按钮之前,它将删除单元格内容视图中的所有按钮

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

请在添加按钮作为子视图之前插入以下代码,在添加新按钮之前,它将删除单元格内容视图中的所有按钮

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

我建议使用标签。请参阅下面的代码

- (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];
    }

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

    return cell;

}

我建议使用标签。请参阅下面的代码

- (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];
    }

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

    return cell;

}

如果您使用Xib,您可以使用

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

if (cell==nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

如果您使用Xib,您可以使用

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

if (cell==nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
您可以删除if(cell==nil)代码块,dequeueReusableCellWithIdentifier已足够。您可以删除if(cell==nil)代码块,dequeueReusableCellWithIdentifier已足够。