2016-04-06 42 views
0

我正在创建一个具有pickerView的自定义单元格,当我运行该应用程序时,它会使用选取器视图加载所有单元格。一次有4个单元格可见,当我更改第一个选择器视图的值并向下滚动时。每个第四选择器的价值已经改变。我得到所有选择器视图的值,它返回正确的值,意味着它只改变第一个选择器视图的值。dequeueReusableCellWithIdentifier更改多个UIPickerviews值

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    AddTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addTaskCell" forIndexPath:indexPath]; 

    AddTaskDetails *task =[self.tasksArray objectAtIndex:indexPath.row]; 
    NSAttributedString *attributedString = [self attributedTextForTask:task.taskDetail]; 
    cell.taskDetails.attributedText = attributedString; 
    cell.hourePicker.tag = indexPath.row; 
    [cell.hourePicker selectRow:0 inComponent:0 animated:YES]; 
    cell.addDescriptionBtn.tag = indexPath.row; 

    [cell.addDescriptionBtn addTarget:self action:@selector(didTapAddDescButton:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; } 

回答

0

浪费了6小时后,我已经解决了这个问题,这里是更新后的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    AddTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addTaskCell" forIndexPath:indexPath]; 

    AddTaskDetails *task =[self.tasksArray objectAtIndex:indexPath.row]; 
    NSAttributedString *attributedString = [self attributedTextForTask:task.taskDetail]; 
    cell.taskDetails.attributedText = attributedString; 
    cell.hourePicker.tag = indexPath.row; 

    cell.addDescriptionBtn.tag = indexPath.row; 

    [cell.addDescriptionBtn addTarget:self action:@selector(didTapAddDescButton:) forControlEvents:UIControlEventTouchUpInside]; 
    AddTaskDetails *task_1 =[self.tasksArray objectAtIndex:indexPath.row]; 
    if(task_1.hours>0) 
    { 
     [cell.hourePicker selectRow:task_1.hours inComponent:0 animated:NO]; 
    } 
    else 
    { 
     [cell.hourePicker selectRow:0 inComponent:0 animated:NO]; 
    } 

    return cell; 
} 
2

发生这种情况的原因是单元格正在被重新使用,但从未使拾取器复位。

在配置要显示的单元格时,需要将选取器设置为默认值,然后将其设置为该单元格的正确值(如果适用)。

您需要将选择器中选定的值存储在表格单元格以外的其他位置。表格单元格将被重新使用,所以你需要一个数据结构(可能是你的数据源)。

+0

我已经做到了。但现在,如果我滚动我的tableview它将所有选择器视图重置为默认值。 –

+0

你能提供你的方法去除细胞吗? 你在哪里存储在选择器中选择的值? – anierzad

+0

好的。我在编辑我的问题。 –

0

设置选择器视图中

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

每个单元格的值,如果你不设置在这里的针对每个小区选择器视图值将是奇怪的。它在滚动后显示相同值的原因是因为iOS只是拾取可见单元格并在滚动时再次显示它们。所以除非您在cellForRowAtIndexPath:中设置值,否则它们将具有以前的值。