【问题标题】:Using delegate methods to save a custom cell textfield使用委托方法保存自定义单元格文本字段
【发布时间】:2016-09-28 17:00:24
【问题描述】:
我无法理解如何使用委托方法从包含在表格单元格中的文本字段中保存文本值。我正在为表格单元格使用一个单独的 .xib 文件,其中包含文本字段。下面是使用 cellForRowAt 索引路径的代码,但我不确定如何在文本字段和包含此代码的 swift 文件之间进行通信。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let currentCellDescriptor = getCellDescriptorForIndexPath(indexPath)
if currentCellDescriptor["cellIdentifier"] as! String == "idEntranceFee" {
func textFieldDidBeginEditing(textField: UITextField!) {
}
}
【问题讨论】:
标签:
ios
delegates
textfield
tableviewcell
【解决方案1】:
这是我用来处理这个问题的代码。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
tableView.registerNib(UINib(nibName: "PostsCell", bundle: nil), forCellReuseIdentifier: "PostsCell")
postsCell = tableView.dequeueReusableCellWithIdentifier("PostsCell") as! PostsCell
let song = album.objectAtIndex(indexPath.row) as! NSArray
let songName = song[0] as? String
postsCell.titleLabel.text = songName
return postsCell
}
这就是我的 postsCell 文件的样子。
class PostsCell: UITableViewCell
{
@IBOutlet weak var titleLabel : UILabel!
@IBOutlet weak var bodyLabel : UILabel!
}
您的问题有点含糊,但这应该可以帮助您。如果您有任何问题,请回复评论。
【解决方案2】:
您必须为 ViewController 中的每个 UITextfield 设置一个委托,在您的情况下是在每个 UITableViewCell 中。
textField.delegate = self
然后在你的 ViewContoller 中实现 UITextFieldDelegate:
extension YourViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(textField: UITextField) {
}
func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath) {
}
}