2014-04-10 115 views
-1

我已經實現了在表處於編輯模式時在tableView中插入新行的方法。 當我按下帶有綠色「加號」圖標的單元格時,一個新的單元格會以綠色的「加號」添加到單元格上方。新單元格包含一個空的textField,它將成爲第一響應者並打開鍵盤。這是我的代碼:保存在tableView中插入新行後成爲第一響應者的UITextField文本

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 

    [super setEditing:editing animated:animated]; 
    if (editing) { 
     [self.tableView beginUpdates]; 
     [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:[itemsArray count] inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tableView endUpdates]; 
    } else { 
     [self.tableView beginUpdates]; 
     [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:[itemsArray count] inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tableView endUpdates]; 

    } 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (indexPath.row >= [itemsArray count]) { 
     return UITableViewCellEditingStyleInsert; 
    } else { 
     return UITableViewCellEditingStyleDelete; 
    } 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.tableView beginUpdates]; 
     [itemsArray removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tableView endUpdates]; 

    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     [self.tableView beginUpdates]; 
     [itemsArray addObject:@""]; 
     [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tableView endUpdates]; 
     ClientCell * cell = (ClientCell*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]; 
     cell.cellText.enabled = YES; 
     cell.cellText.delegate = self; 
     [cell.cellText becomeFirstResponder]; 
    } 
} 

問題:如何保存我在cellArray中的cell.textField中輸入的文本?該單元成爲第一響應者,從那裏我需要一些建議或指導如何將該文本保存在數組中。

回答

1

您已經設置的UITextField的代表在細胞中,因此只需要實現:

- (void)textFieldDidEndEditing:(UITextField *)textField { 
    NSString *text = textField.text; 
    // Do whatever you want with the text, like putting it in the array 
} 

我不知道如果小區有一個按鈕來保存文本或類似的東西。上述方法會在uitextfield失去焦點時觸發。

相關問題