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

时间:2017-07-26 04:16:37

标签: ios swift uitableview

我有2个控制器AddContactControllerHomepageController。我想选择主页中的每一行(从Web服务加载JSON数据)。然后传递每行的数据并导航到AddContactControllerTableViewCell也是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 } } 。我该怎么做 ?我的代码有点凌乱。有什么建议吗?

AddContactController类

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]()

支持所有需要通过数据的商店

service.files().get_media(fileId=fileID).execute()

3 个答案:

答案 0 :(得分:1)

要使用 indexPath (例如tableView.cellForRow(at: indexPath)

>获取选定行的单元格

SampleCode:

  

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
}

答案 1 :(得分:0)

假设您的数据是从webservice收到的数组或字典形式,然后在您的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)    }

答案 2 :(得分:0)

您无法从当前类访问另一个类UITableViewClass。加载AddContactViewController后,您的TableView会被实例化。在实例化之前,您无法访问AddContactViewController子视图。您可以按照数组或字典格式传递@Sakhir和@hussain所需的数据。并在viewDidAppear或AddContactViewController的viewDidLoad中重新加载tableview。您可以将cellforRowAtIndexPath中的textField中的数据设置为textfield.text = yourValue。加载后,您可以编辑或更新数据。希望你能得到它。