2017-07-08 163 views
0

我會做這樣的事情https://i.stack.imgur.com/jAGsk.png保存文本框的值單元格爲textLabel(名稱)SWIFT]

因此,如果用戶輸入點 - 它會點保存到用戶的姓名。怎麼做?我用一個函數將textField粘貼到tableViewCell中。 下面是從tableViewCell代碼文件

@IBOutlet weak var inputScore: UITextField! 

public func configure(text: Int?, placeholder: String) { 
    inputScore.text = String(text!) 
    inputScore.placeholder = placeholder 

    inputScore.accessibilityValue = String(text!) 
    inputScore.accessibilityLabel = placeholder 
} 

這裏是代碼從VC文件

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "InputScore") as! InputScoreTableViewCell 
    cell.textLabel?.text = usersIn[indexPath.row] 
    cell.configure(text: 100, placeholder: "Score") 
    return cell 
} 
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return usersIn.count 
} 

那麼如何將其保存到用戶的姓名?

+0

看看這個解決方案:https://stackoverflow.com/a/42159435/4488252 –

+0

@GOODDUDE SO是一個社區,有些人只是爲了解決其他人的問題而解決一些問題幫助人們作爲感謝的證明,請表示感謝您解答您的答案 –

回答

0

你必須爲用戶創建一個數據模型:

class User: NSObject { 
    var points = 0 
} 

,然後在視圖控制器創建用戶的數組:

var users = [User]() 

這樣一來,你可以做這樣的事情

var user = users[indexPath.row] 
user.points = 100 
print(user.points) // 100 

然後,您可以在您的表格視圖中顯示您的用戶點數。您還可以將tag分配給等於indexPath.row的文本字段,以便您輕鬆使用它們。

0

在由@Cesare提供的使用用戶模型之上,我們需要修改cellForRowAtIndexPath方法和您單元的實施,增加了對數據的變化事件封閉,並用它

@IBOutlet weak var inputScore: UITextField! 
fileprivate var fnDataWasUpdated : (Int?) -> Void = {_ in} //closure for data change notification 

public func configure(text: Int?, placeholder: String,_ fnListener: @escaping (Int?) -> Void) { 
    inputScore.text = String(text!) 
    inputScore.placeholder = placeholder 

    inputScore.accessibilityValue = String(text!) 
    inputScore.accessibilityLabel = placeholder 

    //added delegate implementation for UITextField 
    inputScore.delegate = self 
    self.fnDataWasUpdated = fnListener 
} 

也需要你的細胞採用UITextFieldDelegate協議

extension InputScoreTableViewCell : UITextFieldDelegate 
{ 
    func textFieldDidEndEditing(_ textField: UITextField) 
    { 
     if let intValue = Int(textField.text) 
     { 
      self.fnDataWasUpdated(intValue) 
     } 
    } 
} 

最後我們使用了新的閉包在你的

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "InputScore") as! InputScoreTableViewCell 
    let currUser = self.users[indexPath.row] 
    cell.configure(text: currUser.points, placeholder: "Score",{ (newIntValue) in 
       currUser.points = newIntValue 
      }) 
    return cell 
} 

此代碼是未經測試,但用在幾個項目的主要概念,我一直,所以如果你有任何問題,請讓我知道

我希望這可以幫助你

+0

回答您的第一條評論時,您是否使用過封閉?,當值改變閉包被執行並修改這個單元格的模型用戶的值時,你是否嘗試了我的代碼? –

0

使用DidSelectRowAtIndexPath方法在textField中獲取單元textLable文本。

下面的示例代碼,這個:

import UIKit 

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 

    @IBOutlet var btnOK: UIButton! 
    @IBOutlet var txtValue: UITextField! 
    @IBOutlet var tblData: UITableView! 
    let arrResult = NSMutableArray() 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     tblData.dataSource = self 
     tblData.delegate = self 
     btnOK.tag = 57775 
     btnOK.addTarget(self, action: #selector(applyEdit(sender:)), for: .touchUpInside) 
    } 

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 
     cell.textLabel?.text = arrResult[indexPath.row] as? String ?? "" 
     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     btnOK.tag = indexPath.row 
     let cell: UITableViewCell = tableView.cellForRow(at: indexPath)! 
     txtValue.text = cell.textLabel?.text 
     setTitle() 
    } 

    func setTitle() { 
     if btnOK.tag == 57775 { 
      btnOK.setTitle("Add", for: .normal) 
     }else{ 
      btnOK.setTitle("Update", for: .normal) 
     } 
    } 

    func applyEdit(sender: UIButton) { 
     if sender.tag == 57775 { 
      arrResult.add(txtValue.text ?? "") 
     }else{ 
      arrResult.removeObject(at: sender.tag) 
      arrResult.insert(txtValue.text ?? "", at: sender.tag) 
      sender.tag = 57775 
      setTitle() 
     } 
     txtValue.text = "" 
     tblData.reloadData() 
    } 


} 

輸出:

enter image description here