TableViewCell堆积并出现一次又一次

我用ADLively TableView实现了TableView。

但是,如果我滚动tableView,单元格的文本成为一堆又一堆….

使用“cell.textLabel”是好的,添加UILabel并不好,但如果我使用“cell.textLabel”,我不能调整textLabel的宽度。

(我想在文本的左侧和右侧添加UIImageView)

所以现在我用这种方式来添加UILabel。

我该怎么办?

这是代码。

- (void)viewDidLoad { CGRect rect = CGRectMake(0, 50, 320, self.view.frame.size.height-150); self.tableView = [[ADLivelyTableView alloc]initWithFrame:rect style:UITableViewStylePlain]; self.tableView = (ADLivelyTableView *)self.tableView; self.tableView.initialCellTransformBlock = ADLivelyTransformFan; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.view addSubview: self.tableView]; //self.tableView.backgroundColor = [UIColor blueColor]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.section count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; [self updateCell:cell atIndexPath:indexPath]; if (cell == nil){ myCellView = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; NSMutableArray *set = [self.section objectAtIndex:indexPath.row]; tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)]; tableTitleLabel.text = [set objectAtIndex:0]; [tableTitleLabel setNumberOfLines:0]; tableTitleLabel.backgroundColor = [UIColor clearColor]; tableTitleLabel.textColor = [UIColor blackColor]; tableTitleLabel.font = [UIFont fontWithName:@"HiraKakuProN-W3" size:15]; tableTitleLabel.adjustsFontSizeToFitWidth = YES; tableTitleLabel.minimumScaleFactor = 10.0f; [myCellView.contentView addSubview:tableTitleLabel]; } NSMutableArray *set = [self.section objectAtIndex:indexPath.row]; [(UILabel *)[cell.contentView viewWithTag:1] setText:[set objectAtIndex:0]]; return cell; } - (void)updateCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { NSMutableArray *set = [self.section objectAtIndex:indexPath.row]; tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)]; tableTitleLabel.text = [set objectAtIndex:0]; [tableTitleLabel setNumberOfLines:0]; tableTitleLabel.backgroundColor = [UIColor clearColor]; tableTitleLabel.textColor = [UIColor blackColor]; tableTitleLabel.font = [UIFont fontWithName:@"HiraKakuProN-W3" size:15]; tableTitleLabel.adjustsFontSizeToFitWidth = YES; tableTitleLabel.minimumScaleFactor = 10.0f; [cell.contentView addSubview:tableTitleLabel]; } 

在滚动单元格中添加子视图不是一个好主意,但是如果您不想更改已经编写的代码,请在调用updateCell方法之前,使用以下命令删除该单元格的所有子视图。

  for (UIView *view in [cell subviews]) { [view removeFromSuperview]; }