【问题标题】:How to link two view controllers to a programmatically created table view? [closed]如何将两个视图控制器链接到以编程方式创建的表视图? [关闭]
【发布时间】:2023-03-25 11:18:01
【问题描述】:

对不起,我的英语不好。

我想要这个:当有人点击一个单元格时,它会在另一个视图控制器中打开。

我以编程方式创建了表格视图。

import UIKit
class BibleBooksViewController: UIViewController, UITableViewDelegate, 
UITableViewDataSource {

private let myArray: NSArray = ["Gênesis", "Êxodo", "Levitico"]
private var myTableView: UITableView!
var bibleArray = BibleBooksMock().booksArray

override func viewDidLoad() {
    super.viewDidLoad()

    let barHeight: CGFloat =
UIApplication.shared.statusBarFrame.size.height
    let displayWidth: CGFloat = self.view.frame.width
    let displayHeight: CGFloat = self.view.frame.height

    myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: 
displayWidth, height: displayHeight - barHeight))
    myTableView.register(UITableViewCell.self, forCellReuseIdentifier: 
"MyCell")
    myTableView.dataSource = self
    myTableView.delegate = self
    self.view.addSubview(myTableView)
}

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

func tableView(_ tableView: UITableView, heightForRowAt indexPath: 
IndexPath) -> CGFloat {
    return 70
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 
-> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: 
indexPath as IndexPath)
    cell.textLabel!.text = "\(bibleArray[indexPath.row].title)"
    return cell
 }
}

输出是这样的:

iOS 版本为 12.1 swift版本是4.2

【问题讨论】:

    标签: swift storyboard tableview segue


    【解决方案1】:

    当我们点击单元格时在tableview didselect方法中使用这个代码这个方法用于操作

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
                vc.strText = "\(arrData[indexPath.row])"
                self.present(vc, animated: true, completion: nil)
                 OR
                self.navigationController?.pushViewController(vc, animated: true)
            }
    

    当我们必须传递 ViewController 是 DetailViewController 的数据或导航时

    class DetailViewController: UIViewController {
    
        var strText: String?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            yourlabel.text = strText
        }
    
    }
    

    在我的代码中,我使用 DetailViewController 作为情节提要 ID,因此在情节提要中设置它,就像在 SS 中一样。

    您可以在 youtube 上查看我的视频:- https://www.youtube.com/watch?v=4MOiU_Qop-0

    【讨论】:

    • 如果此答案对您有帮助,请点赞我的答案,以便其他人可以轻松找到它。谢谢
    【解决方案2】:

    将此添加到BibleBooksViewController

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let chapter = bibleArray[indexPath.row]
        var vc = ChapterViewController()
        vc.chapterName = chapter.title
        navigationController?.pushViewController(vc, animated: true)
    }
    

    并定义这个控制器:

    class ChapterViewController: UIViewController {
    
        var chapterName: String?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            title = chapterName
    
            view.backgroundColor = UIColor.white
        }
    
    }
    

    更多关于tableView(_:didSelectRowAt:)here

    【讨论】:

    • " 无法将 'ChapterViewController' 类型的值转换为预期的参数类型 'UIViewController' "
    • 黑屏是这样吗?
    • @LucasOliveiraFerreira 是的,这似乎是对的。您可以更新控制器的视图背景颜色。请参阅我的更新答案。另外,你还得到你一开始提到的错误吗?
    • 嗯,第一个错误是因为我拼错了 chapter.title。
    • @LucasOliveiraFerreira 看看我更新的答案。简而言之,将view.backgroundColor = UIColor.white 添加到ChapterViewControllerviewDidLoad
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多