首页 > 解决方案 > 在表视图的自定义单元格上隐式展开可选值时意外发现 nil

问题描述

我正在尝试从数组中在 tableview 上创建一个自定义单元格。当我在我的自定义单元格上附加任何文件时,我在所有文件上都得到了意想不到的零,我不知道为什么

这是我的自定义单元格:


class CustomMovieCell: UITableViewCell {


    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var rating: UILabel!
    @IBOutlet weak var releaseYear: UILabel!
    @IBOutlet weak var genre: UILabel!
    var imageBackground: String!

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


}

这是我的 tableview cellForRowAtIndexPath 方法:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath) as! CustomMovieCell
        let movieFetched = Movie(title: moviesArray[indexPath.row].title, image: moviesArray[indexPath.row].image, rating: moviesArray[indexPath.row].rating, releaseYear: moviesArray[indexPath.row].releaseYear, genre: moviesArray[indexPath.row].genre)
        print(movieFetched)
        cell.title.text? = movieFetched.title
        cell.rating.text? = String(movieFetched.rating)
        cell.releaseYear.text? = String(movieFetched.releaseYear)
        cell.genre.text? = String(movieFetched.genre[0])
        return cell

    }

我错过了什么?当附加任何文件时,我在展开可选值时意外发现 nil - 我不知道 UIlabel 因为 IBOutlet 是可选的?尽管它们在我的自定义单元类中不是可选的。

标签: iosswiftuitableviewnulloptional-values

解决方案


推荐阅读