2012-06-14 13 views
0

我正在嘗試在iPhone中創建一個類似於iPhone通訊簿的通訊錄。當我想要創建新聯繫人時,在輸入UITextField中的值以輸入電話號碼時,它將在TableView中創建一個新單元格。我已經嘗試了下面的代碼,但它正在創建多個單元格。當我點擊已經在iOS的uitableviewcell中的文本框時如何創建一個新的單元格?

(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
UITableViewCell *parentCell = (UITableViewCell *)[self.tview indexPathsForSelectedRows];  NSIndexPath *currRow = [self.tview indexPathForCell:parentCell]; 

[self addRow:currRow text:textField.text];  

} 


(void)addRow:(NSIndexPath *)indexPath text:(NSString *)text 
{ 
    NSIndexPath *nextRow; 

    if(indexPath.section==0) 
     i++; 
    nextRow = [NSIndexPath indexPathForRow:i-1 inSection:indexPath.section]; 

     [self.tview beginUpdates]; 
     // [self.tview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tview insertRowsAtIndexPaths:[NSArray arrayWithObject:nextRow] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tview endUpdates]; 
} 
+1

您必須保存條目形成文本字段成MutableArray並給予細胞的數量部分作爲[數組計數]。在您退出FirstResponder的函數中,只需重新加載表格,隨着陣列數量的增加,您的細胞數量也會增加 – superGokuN

回答

0
in your code [self.tview indexPathsForSelectedRows]; 
does not return the selected cell while it returns the array of index path ... that's why its creating a multiple row 

做的是定製UITextViewClass並與NSIndexPath一個屬性;
並在索引路徑行單元格爲它分配...

yourTextView.yourIndexPath = indexPath; 

//the try to use this method 

(void)textFieldDidBeginEditing:(UITextField *)textField { 

    [self addRow:indexPath text:textField.text]; 
} 

// then add your row as you want.... 

希望這將幫助你

相關問題