0

我正在尝试在 iPhone 中创建一个地址簿,类似于 iPhone 地址簿。当我想创建一个新联系人时,在输入值UITextField以输入电话号码时,它正在创建一个新的单元格TableView。我已经尝试了下面的代码,但它正在创建多个单元格。

(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];
}
4

1 回答 1

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.... 

希望对你有帮助

于 2012-06-14T10:56:16.120 回答