2012-01-13 63 views
2

我有一个关于可用视图的问题。如何让用户修改UITableView单元格中的文本

我正在实现一个类似于地址簿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; 

}

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

} 
+0

在Xcode中4,您可以使用静态表单元配置和绑定文本字段作为IBOutlets。几乎为零的胶水代码。 – cocoafan 2012-08-13 13:59:35

回答

5

这是一个非常复杂的问题充分和深入的代码示例回答这个问题,但我会尽力为你指明正确的方向。

1)当你在tableView:cellForRowAtIndexPath:方法中创建单元格(我认为这就是你的getCellContentView:方法的目的)时,添加一个UITextField作为你的表格单元格的子视图。在你的UITextField上设置一个与单元格的行索引匹配的标签,并让你的tableviewcontroller成为单元格的代表。将文本字段设置为隐藏。 (记住每次请求单元格时设置标签,而不仅仅是第一次创建它)。

2)在tableView:didSelectRowAtIndexPath:方法中,使用tableViewCellForRowAtIndexPath获取单元格,然后在其中显示文本字段(您可能必须执行一些视图遍历来获取它),并在文本字段上调用becomeFirstResponder。

3)当用户键入内容时,textfielddelegate方法将被触发。您可以查看文本字段上的标记以确定该字段属于哪一行,然后使用新文本更新数据源。然后重新加载表格以隐藏文本字段并更新单元格内容。

如果您知道如何使用自定义表格单元格子类,那么您可以通过创建一个已包含文本字段并具有用于访问它的属性的自定义单元格来使您的生活更轻松一点,但否则该技术将基本相同。另外,tableView:didSelectRowAtIndexPath:当tableview处于编辑模式时通常不会触发,除非您设置了tableView.allowsSelectionDuringEditing = YES;

+0

感谢您的明确解释。但我的问题是在cellForRowAtIndexPath方法,当它处于编辑模式,视图不显示我想要的。我希望我的编辑视图在单元格左侧的标签中显示名称,电话号码,电子邮件(这些3对于每个联系人都是常量),并在单元格右侧的文本字段中显示相应的值。我猜你可以正确地得到我。当视图处于编辑模式时,它仍然显示相同的数据和相同的数据,没有标签和文本框。 – jessy 2012-01-15 06:19:52

+0

根据表格是否处于编辑模式,您可能需要创建一个显示不同子视图的自定义单元格。表格和单元格都具有方法和属性,可以让您检测并在编辑和非编辑模式之间进行切换,但在编辑时委托人无法指定其他单元格。 – 2012-01-15 15:12:44

1

最好使用2 UITableViewCell s,第一个为view,最后一个为edit模式。

此外,我们将取决于指向当前编辑行的变量rowToEdit。 (在我的情况下,一个单元被允许在同一时间进行编辑)

让我们开始吧:

  1. 首先,我靠accessoryButtonTap行动编辑该行:

    var rowToEdit: IndexPath? = nil 
    
    override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { 
        // End edit mode if one cell being in edit mode other than this one 
        if let row = self.rowToEdit { 
        // return current edit cell to view mode 
        self.rowToEdit = nil 
        self.tableView.reloadRows(at: [row], with: .automatic) 
        } 
    
        self.rowToEdit = indexPath 
        self.tableView.reloadRows(at: [self.rowToEdit!], with: .automatic) 
    } 
    
  2. 加载单元格时区分两种模式:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
        if indexPath == self.rowToEdit { 
        let cellId = "ContactEditTableViewCell" 
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath as IndexPath) as! ContactEditTableViewCell 
        cell.accessoryType = .none 
        self.configEditCell(cell: cell, indexPath: indexPath) 
        return cell 
        } else { 
        let cellId = "ContactTableViewCell" 
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath as IndexPath) as! ContactTableViewCell 
    
        self.configCell(cell: cell, indexPath: indexPath) 
        return cell 
        } 
    } 
    
  3. 其他选择,如果你想基于模式改变高度:

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
        if indexPath == self.rowToEdit { 
        return 120 
        } else { 
        return 70 
        } 
    } 
    
  4. 最后选项添加SaveCancel按钮: 我加入他们每个cell,所以我传递一个参考ContactTable每个单元。

    @IBAction func btnSave_click(_ sender: UIButton) { 
        // save the record 
    
        btnCancel_click(sender) 
    } 
    
    @IBAction func btnCancel_click(_ sender: UIButton) { 
        let tmp = self.tbl.rowToEdit 
        self.tbl.rowToEdit = nil 
        self.tbl.tableView.reloadRows(at: [tmp!], with: .automatic) 
    } 
    

    Screenshot

相关问题