You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift开发:电影列表TableView不显示问题求助

问题:Swift项目中TableView无内容显示

我是Swift编程新手,目前开发iOS项目时遇到问题:在视图中用TableView展示电影列表,点击单元格跳转详情页,但运行后TableView没有任何内容。我先想让表格单元格正常显示,再排查后续问题。附上相关代码和终端错误信息,请求帮助。


ViewController代码

import UIKit

class ViewController: UIViewController,
UITableViewDelegate,
UITableViewDataSource  {

let movieList = ["Step Brothers", "Pulp Fiction", "Ali", "Harry Potter"]
let yearList = ["2008", "1994", "2001", "2001"]
let images = ["step_brothers", "pulp_fiction", "ali", "harry_potter3"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let tempCell: TableViewCell = tableView.dequeueReusableCell(withIdentifier:         
             "cell") as! TableViewCell
    
    tempCell.movieTitleLabel.text = movieList[indexPath.row]
    tempCell.movieYearLabel.text = yearList[indexPath.row]
    tempCell.movieImage.image = UIImage(named: images[indexPath.row] + ".jpeg")
    
    return tempCell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    let detailVC:MovieDetailViewController = self.storyboard?.instantiateViewController(withIdentifier: "MovieDetailViewController") as! MovieDetailViewController
    // assign the values to the local variable declared in ProductDetailViewController Class
    detailVC.movieImage = UIImage(named: images[indexPath.row] + ".jpeg")!
  
    // make it navigate to ProductDetailViewController
    self.navigationController?.pushViewController(detailVC, animated: true)
 }  
}

TableViewCell代码

import UIKit

class TableViewCell: UITableViewCell {

@IBOutlet weak var movieTitleLabel: UILabel!

@IBOutlet weak var movieYearLabel: UILabel!

@IBOutlet weak var movieImage: UIImageView!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
 }
}

MovieDetailViewController代码

class MovieDetailViewController: UIViewController {

    @IBOutlet weak var movieDetailImage: UIImageView!
    
    @IBOutlet weak var runtimeLabel: UILabel!
    
    @IBOutlet weak var yearDetailLabel: UILabel!
    
    @IBOutlet weak var directorDetailLabel: UILabel!
    
    var runtime: String!  // holds the product name
    var year: String!  // holds the price
    var movieImage: UIImage! // holds the product image
    var director: String!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        movieDetailImage.image = movieImage
        runtimeLabel.text = runtime
        yearDetailLabel.text = year
        directorDetailLabel.text = director

        // Do any additional setup after loading the view.
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
   }

终端错误信息

2022-11-14 17:39:28.232645-0500 Exercise01[25678:1217794] [Storyboard] Unable to find method -[(null) TableViewCell] 2022-11-14 17:39:28.259975-0500 Exercise01[25678:1217794] [Assert] UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly. Inconsistent configuration may cause problems. navigationController=<UINavigationController: 0x141012400>, navigationBar=<UINavigationBar: 0x142106160; frame = (0 47; 0 50); opaque = NO; autoresize = W; layer = <CALayer: 0x600001d72280>> delegate=0x141012400

排查解决步骤

1. 检查TableView的Delegate和DataSource关联

在Storyboard中选中TableView,打开右侧Connections Inspector,确认delegate和dataSource都连接到ViewController。代码中已实现协议,但未关联的话TableView不会调用相关方法。

2. 确认TableViewCell的Identifier匹配

在Storyboard中选中自定义Cell,检查右侧Attributes Inspector里的Identifier是否为cell,需和代码中dequeueReusableCell(withIdentifier: "cell")的字符串完全一致(大小写敏感)。

3. 修复自定义Cell的类关联

选中Storyboard中的TableViewCell,打开右侧Identity Inspector,确认Class设置为TableViewCell,且Module选择当前项目。终端的Unable to find method -[(null) TableViewCell]错误,大概率是Cell未正确关联自定义类导致。

4. 验证IBOutlet连接

检查TableViewCell中movieTitleLabel、movieYearLabel、movieImage三个IBOutlet是否正确连接到Storyboard对应控件:右键控件看连线是否指向Cell类的对应属性,或右键Cell类属性看连线是否指向控件,避免断连或错连。

5. 可选:纯代码场景补充Cell注册

如果是纯代码创建TableView,需在viewDidLoad中添加注册代码:

tableView.register(TableViewCell.self, forCellReuseIdentifier: "cell")

Storyboard拖拽的TableView无需此步骤。

完成以上操作后,TableView应能正常显示单元格内容,后续可再排查详情页的跳转和数据传递问题。


内容的提问来源于stack exchange,提问作者NewCoderBH1694

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 14:55:21