我有一個關於可用視圖的問題。如何讓用戶修改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