(自定义)UITableViewCell在滚动后混合

我已经在这个问题了几个星期了。

基本上,当我向上/向下滚动TableView内使用在IBdevise的自定义单元格所有的内容被混淆和错位

我已经尝试了多种解决scheme,但无济于事,你将不得不原谅我的代码一点点。

人们不断build议为表格单元格做一个子视图,但我不知道该怎么做= /对iOS开发还是比较新的,所以如果你有一个可能的答案,请你尽可能详细的。

再次,对不起我的代码= /

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier]; NSInteger intCellTag; NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; [[[NSBundle mainBundle] loadNibNamed:@"EventsCustomTVCell" owner:self options:nil] lastObject]; cell = tvCell; self.tvCell = nil; cell.textLabel.backgroundColor = [UIColor clearColor]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.tag = intCellTag; intCellTag++; UIImage *customCellBG = [UIImage imageNamed:@"EventsCustomTableCellBG.png"]; UIImageView *customCellBGImageView = [[UIImageView alloc] initWithImage: customCellBG]; customCellBGImageView.contentMode = UIViewContentModeScaleToFill; cell.backgroundView = customCellBGImageView; [customCellBGImageView release]; [cell.contentView addSubview:imgThumbnail]; [cell.contentView addSubview:lblName]; [cell.contentView addSubview:lblDescription]; [cell.contentView addSubview:lblDate]; } imgThumbnail.image = [UIImage imageNamed:[dictionary objectForKey: @"Thumbnail"]]; lblName.text = [dictionary objectForKey:@"Title"]; lblDescription.text = [dictionary objectForKey:@"Description"]; lblDate.text = [dictionary objectForKey:@"Date"]; return cell; } 

它看起来像你试图混合隐喻定义每个UITableViewCell – 从.xib加载,并手动创build子视图。 这当然没有错,但你可以直接把图像和标签放到tableviewCell中,像这样:

在这里输入图像说明

这里是显示每一行的代码(自然在IB中,您已经为每个要为每个行定制的UIKit对象分配了非零唯一标记)

 #define kImageTag 1 #define kLabel1Tag 2 #define kLabel2Tag 3 #define kLabel3Tag 4 // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"MyTvCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { self.tvCell = nil; [[NSBundle mainBundle] loadNibNamed:@"TvCell" owner:self options:nil]; cell = self.tvCell; } UIImageView *iv = (UIImageView *) [cell viewWithTag:kImageTag]; UILabel *lbl1 = (UILabel *) [cell viewWithTag:kLabel1Tag]; UILabel *lbl2 = (UILabel *) [cell viewWithTag:kLabel2Tag]; UILabel *lbl3 = (UILabel *) [cell viewWithTag:kLabel3Tag]; iv.image = [UIImage imageNamed:@"myimage.png"]; lbl1.text = @"howdy"; lbl2.text = @"there"; lbl3.text = @"foo"; return cell; } 
 iOS Swift: I have done following to resolve my issue let CellIdentifier: String = "CellIdentifier\(indexPath.section)\(indexPath.row)" var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as UITableViewCell? if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: CellIdentifier) } 
 imgThumbnail lblName lblDescription lblDate 

你应该为这些对象分配一个标签(你可以在界面生成器中这样做)。
如果您configuration单元格,您可以查询标记的单元格并设置其属性。

现在你保持对最后一个被添加的单元格的标签的引用,并且每次表格要求新的单元格时,都要更改这些标签。

假设你将标签10分配给interfacebuilder中的标签lblDescription

你会replace

 lblDescription.text = [dictionary objectForKey:@"Description"]; 

 UILabel *lblDescription = (UILabel *)[cell viewWithTag:10]; lblDescription.text = [dictionary objectForKey:@"Description"]; 

编辑:我假设imgThumbnail等是你的单元格的子视图,但你再次添加它们。 如果我的假设是正确的,你应该摆脱[cell.contentView addSubview …]。

如果我错了,你应该摆脱imgThumbnail等视图控制器的实例variables。 每次创build新的单元格时添加单独的UIViews。 就像你用backgroundview做的一样。 但是当您configuration单元格值时,您已经分配了一个标签并使用了该标签。

您可以使用

NSString * cellIdentifier = [NSString stringWithFormat:@“%d”,indexPath.row];

代替

静态NSString * CellIdentifier = @“Cell”;

您正在将相同的子视图对象添加到每个单元格

 [cell.contentView addSubview:imgThumbnail]; [cell.contentView addSubview:lblName]; [cell.contentView addSubview:lblDescription]; [cell.contentView addSubview:lblDate]; 

每个单元格都需要自己的一组子视图对象。