[英]Getting text from array to UITableViewCell
我是 Swift 編碼的新手,我需要幫助。
我在將文本從 UITableViewCell 獲取到另一個視圖控制器時遇到了很大的問題。
我創建了一個數組,但它似乎沒有復制到 UITableViewCell 中?
這是我的代碼
進口 UIKit
var 容器 = likeViewController()
class likeViewController: UIInputViewController, UITableViewDelegate, UITableViewDataSource, icebreakerData { func textChoice(string: String?) { }
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
}
}
在您的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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.