如何让用户修改UITableView单元格中的文本

jes*_*ssy 3 xcode uitableview ios

我有一个关于合适的观点的问题.

我正在实现一个类似于地址簿app的应用程序.我能够以编辑模式呈现表格视图.我想让用户在编辑模式下编辑单元格中的文本.我知道为了编辑单元格中的文本,我需要一个文本字段.我创建了一个文本字段.

我的问题是:

  1. 我该怎么做才能在单元格中显示该文本字段.

  2. 为了在编辑模式下在表视图中显示该文本字段,我需要实现哪些方法.

  3. 完成编辑后,如何更新联系人视图控制器中的数据(包含所有联系人).保存应保留在地址簿中.对于这个问题,我知道我需要实现一些委托方法,但我不知道该怎么做.

请查看以下代码,以便您了解我的问题.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
[tableView setSeparatorColor:[UIColor clearColor]];
//[self.tableView setEditing: YES  animated: YES];


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
if(isEditingOn) {

if(cell == nil)
        cell = [self getCellContentView:CellIdentifier];
     UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
     UITextField *textfield1=(UITextField*)[cell viewWithTag:2];

if(indexPath.row == 0) {
        lblTemp1.text = @"Name";
        textfield1.text = myContact.name;
    }

else if(indexPath.row == 1) {
        lblTemp1.text = @"Phone";
        textfield1.text = myContact.phone;
    }

else if(indexPath.row == 2) {
        lblTemp1.text = @"Email";
        textfield1.text = myContact.email;
}

}

else {    

if(indexPath.row == 0) {
    cell.textLabel.text = myContact.name;
}

else if(indexPath.row == 1) {
    cell.textLabel.text = myContact.phone;
}

else if(indexPath.row == 2) {
    cell.textLabel.text = myContact.email;
}
}


return cell;
Run Code Online (Sandbox Code Playgroud)

}

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

CGRect CellFrame = CGRectMake(0, 0, 60, 20);
CGRect Label1Frame = CGRectMake(10, 10, 180, 25);
UILabel *lblTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame  reuseIdentifier:cellIdentifier] autorelease];
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
CGRect TextFieldFrame=CGRectMake(240, 10, 60, 25);
UITextField *textfield;
textfield=[[UITextField alloc]initWithFrame:TextFieldFrame];
textfield.tag=2;
textfield.placeholder = @"";
[cell.contentView addSubview:textfield];

}
Run Code Online (Sandbox Code Playgroud)

Nic*_*ood 5

这是一个非常复杂的问题,可以通过代码示例全面而深入地回答这个问题,但我会尝试指出正确的方向.

1)在tableView:cellForRowAtIndexPath:方法中创建单元格时,添加一个UITextField作为表格单元格的子视图(我假设这是你的getCellContentView:方法的用途).在UITextField上设置一个与单元格的行索引匹配的标记,并使tableviewcontroller成为单元格的委托.将文本字段设置为隐藏.(记得每次请求单元格时都设置标记,而不仅仅是第一次创建它时).

2)在tableView:didSelectRowAtIndexPath:方法中,使用tableViewCellForRowAtIndexPath获取单元格,然后在其中显示文本字段(您可能需要进行一些视图遍历才能获得它)并在文本字段中调用becomeFirstResponder.

3)当用户键入内容时,将触发textfielddelegate方法.您可以查看文本字段上的标记以确定字段所属的行,然后使用新文本更新dat源.然后只需重新加载表以隐藏文本字段并更新单元格内容.

如果您知道如何使用自定义表格单元子类,那么您可以通过创建一个已经包含文本字段并具有访问它的属性的自定义单元格来使您的生活更轻松,但是否则该技术将大致相同.

另外,tableView:didSelectRowAtIndexPath:当tableview处于编辑模式时通常不会触发,除非你设置tableView.allowsSelectionDuringEditing = YES;