iOS:UItableview 在 dequeueReusableCellWithIdentifier 上重复值

标签 ios objective-c uitableview cocos2d-iphone

我的单元格中有一个编辑文本,当我向上或向下滚动时,它将第一个单元格值设置为最后一个单元格和倒数第二个单元格。我认为这是因为它使单元格出列但我无法解决这个问题。这是我的单元格代码:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _myTableView = tableView;
    AddTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addTaskCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    AddTaskDetails *task =[self.tasksArray objectAtIndex:indexPath.row];
    cell.addHours.delegate = self;
    cell.taskDetails.text =task.taskName;
    _hoursTextField = cell.addHours;
    _hoursTextField.delegate = self;
    cell.addHours.tag = indexPath.row;
    cell.addDescriptionBtn.tag = indexPath.row;
    [cell.addDescriptionBtn addTarget:self action:@selector(didTapAddDescButton:) forControlEvents:UIControlEventTouchUpInside];
    [cell.addHours addTarget:self action:@selector(editStart:) forControlEvents:UIControlEventEditingDidBegin];
    [cell.addHours addTarget:self action:@selector(editEnd:) forControlEvents:UIControlEventEditingDidEnd];
    [cell.addHours addTarget:self
                  action:@selector(textFieldDidChange:)
        forControlEvents:UIControlEventEditingChanged];
    if(task.isTextRed)
    {
       cell.taskDetails.textColor = [UIColor colorWithRed:255 green:0 blue:0 alpha:1.0];
        cell.addHours.textColor = [UIColor colorWithRed:255 green:0 blue:0 alpha:1.0];
    }
    else
    {
        cell.taskDetails.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
        cell.addHours.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];

    }
    if(task.isBillable)
    {
        cell.backgroundColor = [UIColor whiteColor];
    }
    else
    {
        cell.backgroundColor = [UIColor lightGrayColor];
    }
    AddTaskDetails *task_1 =[self.tasksArray objectAtIndex:indexPath.row];
    if(task_1.hours>0)
    {
        NSString *hours = [[NSString alloc]initWithFormat:@"%d",task_1.hours];
        cell.addHours.text = hours;
    }
    else
    {
        cell.addHours.placeholder = @"0";
    }
    NSString *hours = [[NSString alloc]initWithFormat:@"Total Hours: %ld",_totalHours];
    if(_totalHours<=24)
    {
        _labelTotalHours.text = hours;
    }
    return  cell;
}

此屏幕截图具有正确的值:

Correct Image

图中4是自动分配的错误值,这个值也不是数组。

Wrong Image

最佳答案

只是一个简短的概述,所以你得到你的答案

UITableView 经过高度优化,因此只在内存中保留屏幕上可见的行。现在,所有行 Cells 都缓存在 Pool 中,并被重用而不是重新生成。每当用户滚动 UITableView 时,它都会将刚刚隐藏的行添加到池中,并将它们重新用于下一个可见行。

那么,现在,来回答你的问题

在你的方法 cellForRowAtIndexPath 中——

始终重置每个索引的值:

例如。如果您的单元格中有 UILabel - 标签,则在 cellForRowAtIndexPath 中设置它时,您需要为每个条件设置所需的值。如果你想让它为空,那么设置它为“”

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   if(today){
     label = "today";
   }
   else if(yesterday){
     label = "yesterday";
   }
else{
   label = ""; **//this is important**
 }

return cell;
}

代码中的问题

if(task_1.hours>0)
    {
        NSString *hours = [[NSString alloc]initWithFormat:@"%d",task_1.hours];
        cell.addHours.text = hours;
    }
    else
    {
       cell.addHours.text = "";  // YOU NEED TO SET TEXT TO EMPTY AS WELL
        cell.addHours.placeholder = @"0";
    }

关于iOS:UItableview 在 dequeueReusableCellWithIdentifier 上重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290553/

相关文章:

ios - 类别最佳实践

ios - ABPeoplePickerNavigationController 和 UIImagePickerController 在 iPad iOS7 上无法正确显示

iPhone 子类化 View Controller 和 IBOutlet UITableView

ios - 向 UITableView 添加滑动手势

ios - 应用程序更新的 AFNetwoking 或 UIWebview 折旧期限

iphone - 图像应该遵循 iphone 中以前的触摸点?

iOS - 无法从 switch 语句跳转到此 case 标签?

ios - UITableView 在 reloadData 上向上滚动

iphone - :In app purchase and Monotouch 的文档或博客

iphone - 如何在iphone的objective-c中以编程方式调整图像大小