2017-03-09 25 views
1

需要一點點的幫助,請斯威夫特:插入新CustomTableViewCell行中的TableView

我在我的NavigationController一個添加按鈕插入一個新CustomTableviewCell,自定義的tableview單元由一個TextField的。我已經能夠成功添加新的單元格,但是我無法獲取每個文本字段中的所有文本,並將它們追加到數組中。

var datas: [String] = [""]

static var username = ""

@IBAction func addNewName(_ sender: Any) { 
    self.isHandlingAddNewUser = true 
    datas.insert(ViewController.username, at: 0) 
    tableView.beginUpdates() 
    tableView.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .automatic) 
    tableView.endUpdates() 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return datas.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserInputCell 

    if let name = cell.usernameTextField.text{ 
     ViewController.username = name 
    } 

    return cell 
} 

我缺少什麼,我嘗試打印出datas,一切都是空的

+0

是什麼'datas.count'? –

+0

我在頂部var datas創建了一個數組:[String] = [「」],我想要做的只是追加數據中的每個文本字段文本 – SimpiMind

回答

0

你不應更新cellForRowAt中的型號。這應該只填充單元格的文本字段的初始值。如果您想了解更改,您需要爲單元設置另一種機制,以便在文本字段更改時通知視圖控制器。

的基本思想如下:

  • 定義協議(我稱爲UserInputCellDelegate),通過該單元能夠告知視圖變化的控制器;

  • cellForRowAt應該僅使用模型中的值(您的datas)更新單元格中的文本字段。它還將視圖控制器定義爲單元的委託(以接收有關更改值的更新);

  • 更新文本字段時(例如,將「編輯結束」掛鉤爲IBOutlet),單元將通過調用委託方法通知視圖控制器更改,從而通知視圖控制器該更改。

  • 當視圖控制器調用它的didUpdate時,它將相應地更新模型。

這樣:

class ViewController: UITableViewController { 

    var datas = [String]()         // start with empty array 

    @IBAction func didTapAddButton(_ sender: Any) { 
     let indexPath = IndexPath(row: 0, section:0) 
     datas.insert("", at: indexPath.row)     // inserting default value (I'm inserting ""; you can insert whatever you want) 
     tableView.insertRows(at: [indexPath], with: .automatic) 
    } 

} 

// MARK: - UITableViewDataSource 

extension ViewController { 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return datas.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserInputCell 
     cell.delegate = self 
     cell.usernameTextField.text = datas[indexPath.row] 
     return cell 
    } 
} 

// MARK: - UserInputCellDelegate 

extension ViewController: UserInputCellDelegate { 

    func didUpdate(cell: UserInputCell, string: String?) { 
     if let indexPath = tableView.indexPath(for: cell) { 
      datas[indexPath.row] = string ?? ""    // update `datas` with value user edited 
     } 

     // For giggles and grins, let's print the array, so we can see what it's doing. 
     // In production app, this `print` statement would be removed. 

     print("\(datas)") 
    } 

} 

而且

protocol UserInputCellDelegate: class {      // this is class protocol, to allow weak reference 

    /// When text field is updated, cell calls this delegate method to inform it of changes 
    /// to text field value. 
    /// 
    /// - Parameters: 
    /// - cell: Cell containing text field that was updated 
    /// - string: String value entered. 

    func didUpdate(cell: UserInputCell, string: String?) 

} 

class UserInputCell: UITableViewCell, UITextFieldDelegate { 

    weak var delegate: UserInputCellDelegate?    // note this is weak to avoid strong reference cycle 

    @IBOutlet weak var usernameTextField: UITextField! 

    // hooked up to "Editing did end" action for text field in IB 

    @IBAction func didEndEditing(_ sender: UITextField) { 
     delegate?.didUpdate(cell: self, string: sender.text) 
    } 

} 
+0

非常感謝你很多,完美的作品 – SimpiMind

1

您還沒有分配數據給細胞本身。 我想你應該這樣做:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserInputCell 

    cell.usernameTextField.text = datas[indexPath.row] 

    return cell 
} 

我不知道你努力實現與username變量是什麼,但我敢肯定,它不應該在小區配置方法

+0

我不認爲這是因爲OP說數據是空的'datas [indexPath.row]'將不會是 –

+0

先生,反過來說,我想將textfield中的每個文本追加到一個數組,我在var數據上面進行了挖掘:[String] = [「」] – SimpiMind

+1

@SimpiMind這個答案是正確的。 'cellForRowAt'不是設置'ViewController的地方。username'。 – rmaddy

0
var row = ["One", "Two", "Three"] 

@IBAction func addRow(sender: AnyObject) { 
    row.append("Four") 
    tableView.reloadData() 
} 
+0

通常這是一個好主意,說明你的答案中的代碼。告訴你它是如何工作的,或者你在做什麼。這有助於新開發人員準確理解發生了什麼。 –