2012-01-13 64 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

相關問題