我有 2 个 Controller AddContactController
和 HomepageController
。我想选择主页中的每一行(从 Web 服务加载 JSON 数据)。然后传递每一行的数据并导航到 AddContactController
,它也是 TableViewCell
。我该怎么做 ?我的代码有点乱。有什么建议吗?
AddContactController 类
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! AddContactCell
let note = tableView.dequeueReusableCell(withIdentifier: "note") as! Note
myCell.input_field.delegate = self
note.input_field.delegate = self as? UITextViewDelegate
if indexPath.section == 0 {
myCell.nameLabel.text = items[indexPath.row]
if (myCell.nameLabel.text == "Occupation")
{
myCell.input_field.attributedPlaceholder = NSAttributedString(string: "Occupation",
attributes: [NSForegroundColorAttributeName:UIColor(red:0.81, green:0.81, blue:0.83, alpha:1.0)])
}
else if (myCell.nameLabel.text == "First Name"){
myCell.input_field.attributedPlaceholder = NSAttributedString(string: "First Name",
attributes: [NSForegroundColorAttributeName:UIColor(red:0.81, green:0.81, blue:0.83, alpha:1.0)])
}
else {
myCell.input_field.attributedPlaceholder = NSAttributedString(string: "Last Name",
attributes: [NSForegroundColorAttributeName:UIColor(red:0.81, green:0.81, blue:0.83, alpha:1.0)])
}
myCell.myContactController = self
return myCell
}
else if indexPath.section == 1 {
myCell.nameLabel.text = "Place"
myCell.input_field.attributedPlaceholder = NSAttributedString(string: "Place",
attributes: [NSForegroundColorAttributeName:UIColor(red:0.81, green:0.81, blue:0.83, alpha:1.0)])
return myCell
}
else {
return note
}
}
主页 Controller 类
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath as IndexPath) as! RecordCell
myCell.playButton.tag = indexPath.row
myCell.playButton.addTarget(self, action: #selector(playAudio(sender:)), for: .touchUpInside)
myCell.nameLabel.text = nameArray[indexPath.row]
let fullNameArray = nameArray[indexPath.row].components(separatedBy: " ")
let fname = fullNameArray[0].characters.first
let lname = fullNameArray[1].characters.first
var initials: String = ""
if (fname != nil && lname != nil ) {
initials.append(fname!)
initials.append(lname!)
}
myCell.shortname.text = initials
myCell.myTableViewController = self
return myCell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "InstanceStoryBoard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! AddContactController
viewController.
self.navigationController?.pushViewController(viewController, animated: true)
}
假定传递数据所需的所有内容都存储在此
var nameArray = [String]()
最佳答案
使用 indexPath 获取 selected row 的单元格 tableView.cellForRow(at: indexPath)
示例代码:
HomeViewCintroller
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! RecordCell
let storyboard = UIStoryboard(name: "InstanceStoryBoard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! AddContactController
viewController.obj = cell.nameLabel.text
self.navigationController?.pushViewController(viewController, animated: true)
}
AddContactViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell.mytxt.text = obj
return cell
}
关于ios - 将数据从另一个 UITableView 传递到 UITableViewCell 中的文本字段(不使用 Segue),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45317370/