【发布时间】:2020-03-24 14:19:21
【问题描述】:
如何使用自定义单元格中的文本字段中的文本?
这是接收控制器:
class ShowName: UIViewController {
@IBOutlet weak var showName: UILabel!
@IBAction func unwindToShowNameData(_ unwindSegue: UIStoryboardSegue) {
let sourceViewController = unwindSegue.source as! enterName
showName.text = sourceViewController.name
}
@IBAction func unwindToShowName(_ unwindSegue: UIStoryboardSegue) {
}
}
这是发送控制器:
class enterName: UIViewController, UITableViewDataSource, UITableViewDelegate {
var name: String?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell", for: indexPath) as! nameCell
return cell
}
@IBAction func clickSave(_ sender: UIBarButtonItem) {
let cell = nameCell()
name = cell.nameText.text
performSegue(withIdentifier: "passData", sender: self)
}
}
这是带有 TextField 的 Cell 类:
class NameCell: UITableViewCell {
@IBOutlet weak var nameText: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
有什么想法吗?
【问题讨论】:
-
你要做什么任务。解释一下。
-
目前还不清楚你要完成什么。只有一行的表格视图没有意义。表格视图将显示什么?为什么这个类被命名为一次
nameCell,然后是NameCell?请详细解释代码应该做什么。 -
这只是一个示例项目。看起来我在写问题时犯了一个错误。当然名字是nameCell。在我的真实项目中,我有更多的行,其中包含 textFields 和其他内容。这就像添加一个新的联系人,我可以在其中输入一个名字。另一个带有 TableView 的 ViewController 获取数据并显示它。
-
@DilanAnuruddha
-
@vadian 信息够了吗?
标签: swift xcode uitableview textfield