2016-04-06 3 views
0

私はpickerViewを持つカスタムセルを作成しています。私は最初のピッカービューの値を変更し、下にスクロールすると、4つのセルが表示されます。 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

これは、セルが再使用されていてもピッカーがリセットされていないために発生しています。

セルを表示用に設定する場合は、ピッカーをそのセルの正しい値に設定する前に、デフォルト値に設定する必要があります(該当する場合)。

選択した値をテーブルセル以外の場所に保存する必要があります。テーブルセルは再利用されるので、データ構造(おそらくデータソース)が必要です。

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

内の各セルの

+0

は、私はそれを行っています。しかし、今私は私のテーブルビューをスクロールする場合、それはデフォルト値にすべてのピッカービューをリセットします。 –

+0

デキューするためのメソッドを提供できますか? ピッカーで選択した値はどこに保存されていますか? – anierzad

+0

私は自分の質問を編集しています。 –

0

セットピッカービューの値は、各セルのためにここでそれを設定しない場合は、ピッカービューの値は奇妙になります。スクロール後に同じ値が表示されるのは、iOSが表示セルをピックアップしてスクロール時に再び表示するためです。したがって、値をcellForRowAtIndexPath:に設定しない限り、以前の値になります。