2011-02-03 13 views
0

私の現在のプロジェクトでは、各セルにテキストフィールドが含まれているテーブルが必要です。セルとテキストフィールドの数は動的でなければならず、MutuableArrayのデータ数。私はセル内のテキストフィールドを使用していますが、テキストフィールドの値を取得/設定できません。あなたが私を助けたり、少なくとも私が間違ったことを修正してくれるかどうか、私は疑問に思いますか?あらかじめありがとうございました。以下のコードスニペットを参照してください。テーブルセルのUITextField値の取得/設定

// Adds textfield into cell 

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

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSUInteger row = indexPath.row; 
     X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:row]; 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     BOOL bShowSelection = ([curIndex.HasVasteWaarden isEqualToString:@"false"]); 

     if (bShowSelection) { 
      bShowSelection = !([curIndex.DataType isEqualToString:@"Datum"]); 
     } 

     if ([indexPath section] == 0) { 
      if (bShowSelection) { 
       cell.accessoryType = UITableViewCellAccessoryNone; 
      } else { 
       cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
      } 

      UITextField *editField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
      editField.adjustsFontSizeToFitWidth = YES; 
      editField.textColor = [UIColor blackColor]; 
      editField.placeholder = curIndex.Naam; 
      editField.keyboardType = UIKeyboardTypeDefault; 
      editField.returnKeyType = UIReturnKeyNext; 
      editField.backgroundColor = [UIColor whiteColor]; 
      editField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 
      editField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support 
      editField.textAlignment = UITextAlignmentLeft; 
      editField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right 
      editField.tag = [curIndex.UID intValue]; 

      [editField setEnabled: YES]; 
      [cell addSubview:editField]; 

      [editField release]; 
     } 
    } 

    return cell; 
} 

場合によっては、popovercontrollerを使用してデータのリストを表示しています。ユーザーはポップアップの値を選択できます。このコードは、選択した値がある場合に実行されます。セルは、私は、セルを後で使用するために保存されていることを確認しています選択されている

- (void)selectedValue:(NSString *) value { 

    //---update value of the text field --- 
    //The first attempt it doesn't put the text to text field 

    //static NSString *CellIdentifier = @"Cell"; 
    //UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    // 
    //if (cell == nil) { 
    // cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    //} 

    // second attempt it crashes 
    X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:curRow.row]; 
    int index = [curIndex.UID intValue]; 
    UITextField *textField = (UITextField *) [curCell viewWithTag: index]; 
    if (textField) { 
      [textField setText:value]; 
    } 

    [textField release]; 

    [self.popOverController dismissPopoverAnimated:YES]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:indexPath.row]; 

    if (!curIndex) { 
     return; 
    } 

    curRow = indexPath; // saves the selected row 

    if ([curIndex.VasteWaarden count] > 0) { 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     curCell = cell; // saves the selected cell 

     CGRect frame = [cell.superview convertRect:cell.frame toView:self.view]; 

     self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];   
     detailViewController.delegate = self; 
     self.popOverController = [[[UIPopoverController alloc] initWithContentViewController:detailViewController] autorelease];    

     X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:indexPath.row]; 
     self.detailViewController.Values = curIndex.VasteWaarden; 

     [self.popOverController presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    } 
} 

もう一度ありがとうございます。

乾杯、第二のコードで Inoel

答えて

2

あなたがTextFieldをリリースしているスニペット。あなたはそれを保持していないので、これを行うべきではありません。 viewWithTag:シンプルなので、テキストフィールドへの参照を取得するので、textFieldは保持されません。したがって、それが保持された回数を解放するので、retainCountは0になり、テキストフィールドはメモリから解放されます。それで2回目にしようとすると、メモリにテキストフィールドがありません。

だけ削除:

[textField release]; 

を2番目のコードスニペットから。理由を理解できない場合は、メモリ管理に関する記事を読んでください(Googleだけ)。それを完全に理解するまでには時間がかかります。少なくとも私はそれが私にしばらくかかったことを知っています:)

+0

ありがとう、素早く反応します。私はこれを削除し、今それはうまく動作します。 :) – Inoel

関連する問題