2012-03-22 4 views
0

申し訳ありませんが、私はこの質問が以前に尋ねられたことは知っていますが、私の問題は他のものとは異なります。UITableFieldをNSMutableArrayのUITableViewCellに保存します。

数字パッドを閉じた後に配列に保存する必要があるカスタムテーブルビューセルの内部にテキストフィールドがあります。ナンバーパッドには完了ボタンがないので、ナンバーパッドを閉じるためにタッチイベントを実装する必要がありました。つまり、私の-textFieldDidEndEditingは起動していません。ここでは、関連するコードは次のとおりです。あなたのタッチイベントは、キーボードを閉じるときに、私のviewController.mの私viewController.h

itemData *tempItem; //A custom object  
UITableView *itemsListTable; 
NSMutableArray *listData; 

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

     static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 


     UITableViewCell *cell = [itemsListTable 
           dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 

     if (cell == nil) { 
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:SimpleTableIdentifier]; 


     NSInteger row = indexPath.row; 
     tempItem = [listData objectAtIndex:row]; 
     cell.textLabel.text = [tempItem getItemName]; 


     UITextField *quantityTextField = [[UITextField alloc] initWithFrame:CGRectMake(275, 8, 35, 27)]; 
     quantityTextField.keyboardType = UIKeyboardTypeNumberPad; 
     quantityTextField.borderStyle = UITextBorderStyleRoundedRect; 
     quantityTextField.returnKeyType = UIReturnKeyDone; 
     quantityTextField.text = [NSString stringWithFormat:@"%d", tempItem.getItemQuantity]; 
     quantityTextField.textAlignment = UITextAlignmentCenter; 


     [cell addSubview:quantityTextField]; 
     } 


     return cell; 

    } 

-(void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event { 
[[self itemsListTable] endEditing:YES]; 
    } 
- (void)textFieldDidEndEditing:(UITextField *)textField { 

    UITableViewCell *cell = (UITableViewCell*)textField.superview; 

    NSIndexPath *indexPath = [self.itemsListTable indexPathForCell:cell]; 

    tempItem = [listData objectAtIndex:indexPath.row]; 

    NSLog(@"Did End Editing"); 
    [tempItem setItemQuantity:[textField.text intValue]]; //save textfield to object as intValue 

    [listData replaceObjectAtIndex:indexPath.row withObject:tempItem]; //save object to array 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [listData removeObjectAtIndex:indexPath.row]; 
     [itemsListTable reloadData]; 
    }  
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    numberOfItems.text = [NSString stringWithFormat:@"%d",listData.count]; 

    return [self.listData count]; 
} 
+0

また、適切な命名規則のために、すべてのクラスの名前は大文字で始める必要があります。 'itemData'の代わりに' ItemData'を使用します。 – Raptor

+0

@ ShivanRaptor私はUITableViewまたはUITextFieldに関するコードを投稿しました – Boos1993

答えて

2

、また[自己textFieldDidEndEditing:]を呼び出し、最後にどのテキストフィールドにフォーカスがあるかを知る必要がある場合は、textFieldDidBeginEditingデリゲート関数の最初のレスポンダを持つ現在のテキストフィールドに設定したUITextFieldオブジェクトを定義して、参照を保持します。このように:

+0

ありがとう!それは私が尋ねたものにぴったりです!残念ながら私はすべてのセルにテキストフィールドを持っているので別のアプローチが必要だと気がついた;) – Boos1993

+0

私は自分のセルのすべてにテキストフィールドを持っていますが、私は問題はありません.. –

+0

すべてのセルにUITextfieldカスタムセルを作成することを考えています。 – Popeye

関連する問題