2012-03-22 80 views
0

好的,我知道這個問題以前已經問過,但是我的問題與其他問題不同。將UITextField內部的UITableViewCell保存到NSMutableArray

我有一個自定義tableview單元格內的文本字段,我需要在數字鍵盤被解散後保存到一個數組。由於數字鍵盤沒有完成按鈕,我不得不實施觸摸事件來關閉數字鍵盤。這意味着我的-textFieldDidEndEditing不會觸發。下面是相關代碼:

在我viewController.h

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

在我viewController.m

- (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 2012-03-22 02:41:42

+0

@ShivanRaptor我只發佈有關UITableView或UITextField的代碼 – Boos1993 2012-03-22 02:42:19

回答

2

當你觸摸事件駁回鍵盤,也叫[自我textFieldDidEndEditing:]或者如果您需要知道哪個文本字段最後有焦點,請通過定義您設置爲textFieldDidBeginEditing委託函數中具有第一個響應者的當前文本字段的UITextField對象來保留引用。像這樣:

//In .h 
UITextField * txtReference; 

//In .m 
- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
    txtReference = textField; 
} 

//In the function that dismisses the keyboard put: 
[self textFieldDidEndEditing:txtReference]; 
+0

謝謝!這對我所問的問題非常合適!不幸的是我意識到我需要一種不同的方法,因爲我在每個單元格中都有一個文本字段;) – Boos1993 2012-03-22 04:56:04

+0

我在每個單元格中都有一個文本字段,我沒有問題.. – 2012-03-22 13:17:01

+0

如果您在每個單元格中都有UITextfield你想過如果你還沒有製作自定義單元格。 – Popeye 2012-03-27 08:34:51