使用自定义tableview滚动时,Tableview图像会出现问题

我有一个Tableview,我使用customTableview单元格来显示单元格

在单元格中我有5个按钮(评级按钮),当我点击特定按钮时,按钮的图像必须改变其工作正常但当我再次滚动桌面视图时,正在更改为正常评级按钮,为了清晰起见,请参阅以下图像

这是滚动前单击按钮上的图像

这是滚动前单击按钮上的图像

点击评级按钮后,图像会像这样改变

在此处输入图像描述

但是当再次滚动单元格时,它会变为第一个图像

请帮帮我

代码:

- (UITableViewCell )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; dataCell = (DataCel)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (dataCell==nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DataCel" owner:self options:nil]; dataCell = [nib objectAtIndex:0]; } return dataCell; } 

你应该保存按钮图像的状态和你的

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

插入代码以保持更新为ex:

  static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { //create your cell here if it was not created cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; [cell.textLabel setFont:[UIFont boldSystemFontOfSize:13]]; [cell.textLabel setTextColor:[UIColor darkGrayColor]]; [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize:11]]; [cell.detailTextLabel setTextColor:[UIColor lightGrayColor]]; cell.detailTextLabel.numberOfLines = 2; } NSArray *array = [imageArray objectAtIndex:[indexPath row]]; if([[array objectAtIndex:3] isEqual:@"rate3"]) { //set the code for the rating 3 } else { //insert another rate depending on your object } 

这是因为每次滚动UITableView ,它都会从您提供的数据中刷新其值! 所以价值不会保留。 为了防止每次滚动数据库时都发生这种情况,您可以使用它

 static NSString *cellIdentifier = @"Identifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; } for(id obj in cell.contentView.subviews) { if([obj isKindOfClass:[UIImageView class]] || [obj isKindOfClass:[UILabel class]]) { [obj removeFromSuperview]; } } 

这不会让值重叠。

滚动时,将重新创建单元格,因此您需要将选定的值保留在单独的数组中,并将其设置在cellForRowAtIndexPath方法中

这是因为每次滚动tableview时,它都会重新创建tableview单元格。 因此,您可以使用数组重用它。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId = @"cellId"; UITableViewCell *cell = [categoriesTableview dequeueReusableCellWithIdentifier:cellId]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } for (UIView *subview in [cell.contentView subviews]) { [subview removeFromSuperview]; } // ui goes here return cell }