2016-04-06 75 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:中設置值,否則它們將具有以前的值。