TableViewCell 堆积如山,一而再再而三地出现 [英] TableViewCell is piled up and appear again and again

查看:23
本文介绍了TableViewCell 堆积如山,一而再再而三地出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ADLively TableView 实现了 TableView.

I implemented TableView with ADLively TableView.

但是如果我滚动 tableView,单元格的文本就会一次又一次地堆积起来......

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

使用cell.textLabel"很好,添加 UILabel 不如使用cell.textLabel",但如果我使用cell.textLabel",我无法调整 textLabel 的宽度.

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.

(我想在文字的左右两边添加UIImageView)

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

那么现在,我使用添加UILabel的方式.

So now, I use the way to add 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 方法之前删除该单元格的所有子视图.

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

这篇关于TableViewCell 堆积如山,一而再再而三地出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆