簡體   English   中英

從自定義表視圖單元格獲取文本字段值

[英]Getting Textfield Values from Custom Table View Cell

從服務器將項目加載到動態表視圖中……我需要為通過文本字段輸入的每個項目發回數量編號。 我無法為文本字段創建出口,因為它給了我錯誤:出口無法連接到重復內容。

我還嘗試使用標簽引用文本字段( UITextField *countTextfield = (UITextField *)[cell viewWithTag:4]; ),但這無法獲取我鍵入的值。

當我單擊保存計數時,我想以以下形式發送回json:

{“ InventoryID”:“ 1231”,“ manual_quantity”:234} {“ InventoryID”:“ 232”,“ manual_quantity”:23} {“ InventoryID”:“ 214”,“ manual_quantity”:1241} {“ InventoryID”: “ 241”,“ manual_quantity”:1123241}

其中manual_quantity是在文本字段中鍵入的值

請查看我鏈接的圖片

圖片

圖片

對我來說,通知表視圖單元更改的方法是為表視圖單元定義一個protocol ,以通知它們的更改並在視圖控制器中實現該協議,該通知將被通知。

  1. 定義協議。
  2. 在視圖控制器中實現protocol
  3. 在表視圖單元格中定義類型為protocol的成員。
  4. UITableView cellForRowAt或任何方便的方法中,將類型為protocol的表視圖單元格成員設置為self (即,實現protocol的視圖控制器)。

使用此protocol機制,表視圖單元可以將其更改及其實際值通知給實現該protocol任何類。

使用CellDelegate協議的偽代碼:

在視圖控制器中:

// View controller
protocol CellDelegate {
    func updateQuantity(inventory: String, quantity: Int)
}

class MyUIViewController: UIViewController, CellDelegate {

    func updateQuantity(inventory: String, quantity: Int) {
        // Update quantity for given inventory
    }

}

在表格視圖單元格中:

public class MyCell: UITableViewCell {
...

@IBOutlet weak var inventory: UIText!
@IBOutlet weak var quantity: UIText!

var cellDelegate: CellDelegate?

// Table view cell
@IBAction func textChanged(_ sender: Any) {
    // inventory and quantity being outlets in the table view cell.
    cellDelegate.updateQuantity(inventory, quantity)
}

道歉的偽代碼有點敏捷。

首先將標簽設置為cellForRowAtindexPath中的textField。 並將委托設置為textField。

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

    cell.textField.tag = indexpath.row
    cell.textField.delegate = self

    return cell
}

現在,用戶每次寫入時使用Tag從textField中獲取數據。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        let result = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? string

        let Dic:NSMutableDictionary = YourArrayName.object(at: textField.tag) as! NSMutableDictionary
        Dic.setValue(result, forKey: "InventoryID")

        YourArrayName.replaceObject(at: textField.tag, with: Dic)

        return true
    }

您可以在自定義單元格中使用塊屬性。 在那里,您可以通過textFieldDidEndEditing方法中的回調發送數據。

您可以通過UITextFieldDelegate協議的委托方法將文本存儲到任何可變數組中。 像下面的方法。

  func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    //when you press return key it will save the text to your mutable array
    textField.endEditing(true)
    self.textFieldValueArray.add(textField.text!)
    return true
  }

並在您的tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)委托方法中使用

  cell.textField.delegate = self

像下面。

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CELL", for: indexPath) as! TableViewCell
    cell.textField.delegate = self

    return cell
  }

最后,您可以像下面那樣使用文本值。

 @IBAction func btnPressed(_ sender: Any) {
    //here textFieldValueArray is a mutable array
    for each in textFieldValueArray {
        print(each)
    }
}

最后,您可以使用幾個UITextFieldDelegate方法來實現您的目的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM