访问 tableView 中的文本字段 SWIFT
accessing a text field in a tableView SWIFT
我有一个tableView。当单元格为 tapped/selected 时,单元格向下扩展以显示多个资产。一项资产是文本字段。另一个是 "submit" 按钮。
我需要在按下“提交”按钮时访问文本字段中的用户输入。我该怎么做?
重要提示:我无法使用 didSelectRowAtIndexpath,因为该单元格已经 selected/expanded。
目前我在 customCell 上有以下内容:
@IBAction func submitButtonPressed(sender: UIButton) {
// prepareForSegue is on mainVC
self.textString = self.textField.text
println(self.textString)
}
在 MainVC 的 "prepareForSegue" 上,我有以下内容:
var cell : CustomCell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as! CustomCell
let path = self.tableView.indexPathForSelectedRow()!
println(cell.textField.text)
self.newText = cell.textString
self.newInt = self.newText.toInt()
println(self.newText)
println(self.newInt)
下面代码正常prints(self.textString)
然而,
cell.textString
、self.newText
和 self.newInt
总是 nil
。
知道为什么吗?
这是示例代码。
@IBAction func submitButtonPressed(sender: UIButton) {
// prepareForSegue is on mainVC
let indexPath = self.tableView!.indexPathForSelectedRow()!
let selectedCell = self.tableView!.cellForRowAtIndexPath(selectedIndexPath!) as! UICustomCell!//your custom cell class.
self.textString = selectedCell.textFied.text //textFied is your textfield name.
println(self.textString)
}
对于 swift 2,此代码基于行索引
let indexPath = NSIndexPath(forRow: 1, inSection: 0) // This defines what indexPath is which is used later to define a cell
let selectedCell = sptableViewObj.cellForRowAtIndexPath(indexPath) as! AddBillerTableViewCell!
self.textFieldTwo = selectedCell.spTextfield.text //textFied is your textfield name.
print(self.textFieldTwo)
我有一个tableView。当单元格为 tapped/selected 时,单元格向下扩展以显示多个资产。一项资产是文本字段。另一个是 "submit" 按钮。
我需要在按下“提交”按钮时访问文本字段中的用户输入。我该怎么做?
重要提示:我无法使用 didSelectRowAtIndexpath,因为该单元格已经 selected/expanded。
目前我在 customCell 上有以下内容:
@IBAction func submitButtonPressed(sender: UIButton) {
// prepareForSegue is on mainVC
self.textString = self.textField.text
println(self.textString)
}
在 MainVC 的 "prepareForSegue" 上,我有以下内容:
var cell : CustomCell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as! CustomCell
let path = self.tableView.indexPathForSelectedRow()!
println(cell.textField.text)
self.newText = cell.textString
self.newInt = self.newText.toInt()
println(self.newText)
println(self.newInt)
下面代码正常prints(self.textString)
然而,
cell.textString
、self.newText
和 self.newInt
总是 nil
。
知道为什么吗?
这是示例代码。
@IBAction func submitButtonPressed(sender: UIButton) {
// prepareForSegue is on mainVC
let indexPath = self.tableView!.indexPathForSelectedRow()!
let selectedCell = self.tableView!.cellForRowAtIndexPath(selectedIndexPath!) as! UICustomCell!//your custom cell class.
self.textString = selectedCell.textFied.text //textFied is your textfield name.
println(self.textString)
}
对于 swift 2,此代码基于行索引
let indexPath = NSIndexPath(forRow: 1, inSection: 0) // This defines what indexPath is which is used later to define a cell
let selectedCell = sptableViewObj.cellForRowAtIndexPath(indexPath) as! AddBillerTableViewCell!
self.textFieldTwo = selectedCell.spTextfield.text //textFied is your textfield name.
print(self.textFieldTwo)