将数据从另一个UITableView (不使用Segue)传递到UITableViewCell中的文本字段



我有 2 个控制器AddContactControllerHomepageController.我想选择主页中的每一行(从 Web 服务加载 JSON 数据(。然后传递每行的数据并导航到同样TableViewCellAddContactController。我该怎么做?我的代码有点乱。有什么建议吗?

添加联系人控制器类

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
}
}

主页控制器类

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获取选定行的单元格,如下所示tableView.cellForRow(at: indexPath)

示例代码:

首页视图

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)    
}

添加联系人视图控制器

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{    
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell.mytxt.text = obj
return cell
}

假设您的数据是从Web服务收到的数组或字典形式,然后在HomepageController中声明一个对象,然后像这样传递数据:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "InstanceStoryBoard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! AddContactController
viewController.details = [some object]
self.navigationController?.pushViewController(viewController, animated: true)    }

您不能从当前类访问另一个类 UITableViewClass。 一旦加载了 AddContactViewController,您的 TableView 就会被即时化。在实例化之前,不能访问 AddContactViewController 子视图。您可以按照数组或字典格式传递所需数据,如@Sakhir和@hussain所述。并在 viewDidSeem 或 viewDidLoad of AddContactViewController 中重新加载您的表视图。您可以在cellforRowAtIndexPath的文本字段中将数据设置为textfield.text = yourValue。加载后,您可以编辑或更新数据。希望你明白了。

最新更新