【问题标题】:UITableView Not Loading Data From SegueUITableView 没有从 Segue 加载数据
【发布时间】:2021-03-20 17:32:01
【问题描述】:

我有一个用户登录的序列,然后他们被带到通过 segue 显示 UITableView 的视图。成功登录后,我正在尝试将登录屏幕中的数据注入表中。

在登录视图中...

    func transitionToHome() {
    print("Hey you logged in!")=
        
    performSegue(withIdentifier: "loginSuccess", sender: self)
    
    self.navigationController!.setNavigationBarHidden(false, animated: false)
    
}

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let vc = segue.destination as! ViewController
    vc.models = [(title: "title", note: "Note")]
}
    

在主屏幕视图中

@IBOutlet var table: UITableView!
@IBOutlet var label: UILabel!

@IBOutlet weak var newNoteButton: UIButton!

var models: [(title: String, note: String)] = [] 

override func viewDidLoad() {
    super.viewDidLoad()
    table.delegate = self
    table.dataSource = self
    title = "Notes"

}

我尝试在 viewDidLoad 以及 viewDidAppear 和 viewWillAppear 中调用 table.reloadData()。两者都没有奏效。

我还在 viewDidLoad 中打印出模型,并且我看到数据正在正确地传递给视图控制器。但是,当从 segue 加载视图控制器时,我无法让表加载这些数据。

【问题讨论】:

  • 如果数据被正确地传递给视图控制器,那么问题与表视图数据源有关。元组作为数据源是不好的做法。
  • @vadian 设置数据源的最佳方法是什么?抱歉,我对 iOS 开发非常陌生,我一直在尝试通过 YouTube 和 Stack Overflow 学习。我正在从 firestore 导入数据
  • 推荐的方式是自定义结构

标签: ios swift


【解决方案1】:

如果您在 viewDidLoad() 中正确建模数据打印,您应该能够使用 dataSource 实现中的数据配置表格视图单元格。请参阅下面的 dataSource 实现的示例扩展。确保为情节提要中的原型单元提供重用标识符。

extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    var content = cell.defaultContentConfiguration()
    content.text = "This is a cell primary text"
    cell.contentConfiguration = content
    return cell
}

【讨论】:

    猜你喜欢
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多