【问题标题】:Getting text from array to UITableViewCell从数组中获取文本到 UITableViewCell
【发布时间】:2020-09-30 12:50:30
【问题描述】:

我是 Swift 编码的新手,我需要帮助。

我在将文本从 UITableViewCell 获取到另一个视图控制器时遇到了很大的问题。

我创建了一个数组,但它似乎没有复制到 UITableViewCell 中?

这是我的代码

导入 UIKit

var container = likeViewController()

likeViewController 类:UIInputViewController、UITableViewDelegate、UITableViewDataSource、icebreakerData { func textChoice(字符串:字符串?){ }

var likedList: [String] = ["Hello there"]

@IBOutlet var tableView: UITableView!


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return likedList.count
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel!.text = likedList[indexPath.row]
        return cell

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        let row = indexPath.row
        self.textDocumentProxy.insertText(likedList[row])

    }


//-----------------

    func tableView(_ tableView: UITableView,
             heightForRowAt indexPath: IndexPath) -> CGFloat {
     // Make the first row larger to accommodate a custom cell.
        return 80
     }



@IBAction func ad(_ sender: Any) {
    container = likedViewController()
    container.likedList.append("I am another one")
        print("I am working")
}


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.dataSource = self
        tableView.delegate = self
    }
func hey (){
    let selectionVC = storyboard?.instantiateViewController(withIdentifier: "KeyboardViewController") as? KeyboardViewController
    selectionVC?.iceBreakerDataDelegate = self
}

}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    在您的func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 中,您可以创建新的 VC 并将其推送到您的 UINavigationController。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    
        let row = indexPath.row
        self.textDocumentProxy.insertText(likedList[row])
    
        // if this is your destination VC
        let selectionVC = storyboard?.instantiateViewController(withIdentifier: "KeyboardViewController") as? KeyboardViewController
    
        // hand over the data
        selectionVC?.iceBreakerDataDelegate = self
        navigationController?.pushViewController(selectionVC, animated: true)
    
        }
    

    如果你没有使用NavigationController,你可以简单地展示viewcontroller。 self.present(selectionVC, animated: true, completion: nil)

    【讨论】:

    • 感谢您的快速回复。我现在收到这条消息。
    • @ManasK 你有 3 个错误。 1. 您试图在 icebreaker 对象上调用 insertText,但它没有该功能。您确定该对象具有该功能吗? 2. 二是代表分配不正确。您需要再次检查类型。 3. 函数中有一个额外的.。应该是navigationController?.pushViewController(selectionVC, animated: true)
    猜你喜欢
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多