TableViewCell is piled up and appear again and aga

2019-09-19 01:30发布

问题:

I implemented TableView with ADLively TableView.

But if I scroll tableView, the cell's texts become to be piled up again and again....

Using "cell.textLabel" is good, adding UILabel is not good than that but if I use "cell.textLabel", I can't resize width of textLabel.

(I want to add UIImageView on the left and right side of text)

So now, I use the way to add UILabel.

What should I do?

Here is the code.

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

回答1:

It is not a good idea to keep adding subviews in cell on scroll but if you do not want to change the code that you have already written, use the following to remove all subviews of that cell before you call you updateCell method.

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