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

标签 ios swift uitableview

我有 2 个 Controller AddContactControllerHomepageController。我想选择主页中的每一行(从 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/

相关文章:

ios - 使用 JSON 进行基本身份验证

objective-c - iOS:如何将 UIViewAnimationCurve 转换为 UIViewAnimationOptions?

ios - 使用 NSFetchedResultsControllers 遍历一对多关系

ios - UITableView : select row with checkmark but no highlight

ios - UICollectionViewCell 如何使用事件单元格停止其他单元格中的某些进程

ios - Admob 广告与 Firebase 集成问题

ios - UICollectionView - 仅在需要时注册 Nib

ios - 为什么我的 iPad 布局不会在旋转时重新调整?

ios - UISearchController 在表头 View 中呈现的 UISearchBar 在事件时动画太远

iphone - 获取非英文字符作为英文字符