我有一个关于可用视图的问题。如何让用户修改UITableView单元格中的文本
我正在实现一个类似于地址簿app的应用程序。我可以在编辑模式下显示表格视图。我想让用户在编辑模式下编辑单元格中的文本。我知道为了编辑单元格中的文本,我需要一个文本框。我创建了一个文本框。
我的问题是:
我应该怎样才能做到以呈现文本框的细胞。
我需要实现哪些方法才能在编辑模式下在表格视图中显示该文本字段。
一旦我完成编辑,如何更新我的联系人视图控制器(包含所有联系人)中的数据。保存应该保留在地址簿中。对于这个问题,我知道我需要实现一些委托方法,但我不知道如何做到这一点。
请看下面的代码,以便您对我的问题有一个了解。
- (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;
}
- (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];
}
在Xcode中4,您可以使用静态表单元配置和绑定文本字段作为IBOutlets。几乎为零的胶水代码。 – cocoafan 2012-08-13 13:59:35