UITutViewCells与UIButton重叠时滚动

我需要创build一个表视图与一些button作为其元素,视图获取加载罚款,但同时向下滚动button重叠。 我认为旧的button不清除,新的button放在它上面。 请帮我解决这个问题。 提前致谢。

- (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 *)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 *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"index path %ld",(long)indexPath.row); NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell = nil; 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; } 

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

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

我build议使用这个标签。 请参阅下面的代码。

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

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

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