UITableviewcell内容更新反映到其他单元格

我在桌面视图中使用8个单元格。 每个单元格都有一组组件,如UIButton,UILabel和UISlider。 当我更新第六个单元格时,它反映到第一个单元格。 同样,如果我更新第七个单元格,它反映到第二个单元格。

删除单元格代码:

{ [buttonarray removeObjectAtIndex:delpath.row]; viewcount--; ListCell1 *cell=(ListCell1 *)[table cellForRowAtIndexPath:delpath]; cell.slider.value=0; cell.qlabel.text=@"1"; cell.slabel.text=@"1"; cell.n=1; [cell.button1 setTitle:@"240" forState:UIControlStateNormal]; for (int m=0; m<7; m++) { [[cell.myarray objectAtIndex:m]setImage:[UIImage imageNamed:@"Num2.png"] forState:UIControlStateNormal]; UIButton *but=[cell.myarray objectAtIndex:m]; but.tag=0; } [self->table deleteRowsAtIndexPaths:[NSArray arrayWithObjects:delpath, nil] withRowAnimation:UITableViewRowAnimationFade]; [table reloadData]; delpath=NULL; } 

当按下button时

 -(void)buttonpressed:(id)sender { viewcount++; table.delegate=self; table.dataSource=self; //UIView *middleview=[[UIView alloc]initWithFrame:CGRectMake(100,200,300,200)]; [buttonarray addObject:sender]; [table reloadData]; } 

代码为tableview

 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return viewcount; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *MyIdentifier=@"mycell"; ListCell1 *cell = (ListCell1 *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[ListCell1 alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; UIButton *b=[buttonarray objectAtIndex:indexPath.row]; cell.imageView.frame=CGRectMake(0, 0, 0, 0); b.imageView.frame=CGRectMake(0, 0, 30, 30); cell.imageView.image=b.imageView.image; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; ListCell1 *cell1 = (ListCell1 *)[tableView cellForRowAtIndexPath:indexPath]; [cell1 addSubview:delbutton]; delpath=indexPath; return; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100.0; } 

发生这个问题是因为UIKit重新使用UITableView中的单元格。 因此,将UI组件添加到该单元格时,相同的元素将在不同的行上重新使用。

简而言之,每次更新单元格时都需要清除/重buildUI。