2017-12-03 5 views
0

私は、UITextField、UIDatePicker、UIPickerViewを持つカスタムセルを持つテーブルビューを持っています。 私のメソッドcellForRowAtIndexPathでは、すべての行に対してその動作を設定しました。 しかし、私は上の最初の行をヒットするとすべてがうまくいきました。代わりに、テーブルをスクロールして下の値を変更すると、たとえば7行目に入力した値が8行目に挿入されます実際に下行 はそれがテーブルをリロードする際9行目に、私はそれを割り当てる または例えばILのUITableViewCellAccessoryDisclosureIndicator代わりに表をスクロールラインのインデックスに混乱を作成し、テーブルは、ラインに割り当て2異なるオブジェクトを持つ動的なUITableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    UITextField *text1 = (UITextField *)[cell viewWithTag:100]; 
    UILabel *text2 = (UILabel *)[cell viewWithTag:200]; 
    text1.text = [valuesArray objectAtIndex:indexPath.row]; 
    text2.text = [titlesArray objectAtIndex:indexPath.row]; 

    switch (indexPath.row) { 
     case 0: 
      { 
       text1.delegate = self; 
       text1.tag = indexPath.row; 
      } 
      break; 

     case 1: 
      { 
       activityPicker = [[UIPickerView alloc] init]; 
       activityPicker.delegate = self; 
       activityPicker.tag = indexPath.row; 
       text1.delegate = self; 
       text1.inputView = activityPicker; 
      } 
      break; 

     case 2: 
      { 
       datePicker = [[UIDatePicker alloc] init]; 
       datePicker.datePickerMode = UIDatePickerModeDate; 
       [datePicker addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; 
       text1.delegate = self; 
       text1.inputView = datePicker; 
       datePicker.tag = indexPath.row; 
      } 
      break; 

     case 3: 
      { 
       datePicker = [[UIDatePicker alloc] init]; 
       datePicker.datePickerMode = UIDatePickerModeTime; 
       [datePicker addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; 
       text1.delegate = self; 
       text1.inputView = datePicker; 
       datePicker.tag = indexPath.row; 
       NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
       [dateFormatter setDateFormat:@"HH:mm"]; 
       [datePicker setDate: [dateFormatter dateFromString:text1.text]]; 
      } 
      break; 

     case 4: 
      { 
       activityPicker = [[UIPickerView alloc] init]; 
       activityPicker.delegate = self; 
       activityPicker.tag = indexPath.row; 
       NSString *km = [text1.text substringToIndex:[text1.text length]-3]; 
       int numkm = [km intValue] - 1; 
       [activityPicker selectRow:numkm inComponent:0 animated:YES]; 
       text1.delegate = self; 
       text1.inputView = activityPicker; 
      } 
      break; 

     case 5: 
      { 
       activityPicker = [[UIPickerView alloc] init]; 
       activityPicker.delegate = self; 
       activityPicker.tag = indexPath.row; 
       text1.delegate = self; 
       text1.inputView = activityPicker; 
      } 
      break; 

     case 6: 
      { 
       activityPicker = [[UIPickerView alloc] init]; 
       activityPicker.delegate = self; 
       activityPicker.tag = indexPath.row; 
       if ([text1.text isEqualToString:[animalsArray objectAtIndex:0]]) 
        [activityPicker selectRow:0 inComponent:0 animated:YES]; 
       else 
        [activityPicker selectRow:1 inComponent:0 animated:YES]; 

       text1.delegate = self; 
       text1.inputView = activityPicker; 
      } 
      break; 

     case 7: 
       text1.delegate = self; 
       text1.tag = indexPath.row; 
      } 
      break; 

     case 8: 
      { 
       activityPicker = [[UIPickerView alloc] init]; 
       activityPicker.delegate = self; 
       activityPicker.tag = indexPath.row; 
       if ([text1.text isEqualToString:[privacyArray objectAtIndex:0]]) 
        [activityPicker selectRow:0 inComponent:0 animated:YES]; 
       else 
        [activityPicker selectRow:1 inComponent:0 animated:YES]; 

       text1.delegate = self; 
       text1.inputView = activityPicker; 
      } 
      break; 

     case 9: 
     { 
      text1.enabled = NO; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
      break; 

     default: 
      break; 
    } 

    return cell; 
} 

答えて

2

問題はすべての行に同じセル識別子"Cell"を使用しています。これにより、すでに特定のセルがあるrowを再ロードするために&を別の行に再利用するように設定したセルが発生します。

あなたが行うことができ、あなただけのこれらの10のセル設計を持っていると仮定すると:

NSString*  identifier = [NSString stringWithFormat: @"Cell-%d", indexPath.row]; 
UITableViewCell* cell  = [tableView dequeueReusableCellWithIdentifier:identifier 
                   forIndexPath:indexPath]; 

このように、dequeueReusableCellWithIdentifierは、現在のindexPathのために以前に作成されたセルを返します。

+0

しかし、ストーリーボードにセル識別子を設定するにはどうすればよいですか? – swifferina

+0

https://stackoverflow.com/a/32397182/1971013 –

+0

はい私はストーリーボードでプロトタイプのセルを接続する方法を知っていますが、この場合は動的なセル識別子を記述する必要があります.... cell1 cell2 cell2 – swifferina

関連する問題