search

Home  >  Q&A  >  body text

objective-c - iOS 自定义tableView cell重用导致内容重复 问题 如何有效解决?急急急!!


 [self.tableView registerNib:[UINib nibWithNibName:@"NewTaskCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NewTaskCellId];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewTaskCell *cell = [self.tableView dequeueReusableCellWithIdentifier:NewTaskCellId forIndexPath:indexPath];
    if (!cell) {
        cell = [[NewTaskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NewTaskCellId];
    }
    if (self.taskList.count > indexPath.row) {
        FindDisOrderInfoEntity *entity = [self.taskList objectAtIndex:indexPath.row];
        [cell setCellContentWith:entity];
    }
    return cell;
}
大家讲道理大家讲道理2772 days ago498

reply all(7)I'll reply

  • 迷茫

    迷茫2017-04-18 09:59:24

    Remove if (self.taskList.count > indexPath.row)
    Cell is assigned normally
    When the cell is added, refresh the table or refresh the new row

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:59:24

    You should first look at the number of your cells. Secondly, the concept of cells is containers. Reuse is just reusing containers. The decision lies with the data source

    reply
    0
  • 阿神

    阿神2017-04-18 09:59:24

    You have already registered using the registerNib method. There is no need to determine whether the cell is empty in the proxy method

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:59:24

    Write this in the (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath proxy method:

    NewTaskCell *cell = [self.tableView dequeueReusableCellWithIdentifier:NewTaskCellId forIndexPath:indexPath];
    FindDisOrderInfoEntity *entity = [self.taskList objectAtIndex:indexPath.row];       [cell setCellContentWith:entity];
    return cell;

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:59:24

    Cell reuse, the previous state is reused, such as the UIButtonUILabel 如果在重用之前UIButton是高亮状态 UILabel上的text=@"label"那么重用之后,UIButton还是高亮状态,UILabeltext还是labelcell重用的时候会调用cellForRow方法。所以一般会在cellForRow里面重新赋值和改变状态。所以在cell里面有个默认的规则 就是如果有if 那一定要有else on the cell, otherwise the cell will become messy.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:59:24

    After registration, you don’t have to judge whether the cell is empty, but there is nothing wrong with it. Why does your count need to be judged? I don’t understand. Maybe your array is not a data source? But it probably isn't your problem. If your "duplication of content" means that the controls on the cell are added repeatedly, then it should be [cell setCellContentWith:entity]; There may be something wrong with the method in this. It may be that you are Create the control here and add it. Reuse only refreshes the data, that is, only the assignment process does not create the control

    reply
    0
  • 迷茫

    迷茫2017-04-18 09:59:24

    If the tableview doesn’t display much, it won’t be repeated if you don’t let it be reused. Also, after calling the registerNib method, the memory will always be in CELL

    reply
    0
  • Cancelreply