我用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];
 }

关于ios - TableViewCell被堆积并一次又一次地出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36858493/

10-12 12:54
查看更多