iOS-如何在 UITableview 中使用数组快速在一个原型(prototype) Cell 中显示 40 张图像?

标签 ios swift uitableview

我正在创建在线教程应用程序。我有 40 个问题。我想使用绿色图像显示正确答案,使用红色图像显示错误答案。我需要在单个图像中显示这 40 个图像原型(prototype)细胞。我想快速了解。

最佳答案

表格 View 旨在显示滚动列表中的多个项目,而不仅仅是一个项目。听起来更像是您想要一个包含 40 行的表格 View ,每一行都显示问题的答案。您可以像这样轻松设置:

// This is your prototype cell configured in interface builder
class ResultTableViewCell: UITableViewCell {

    @IBOutlet weak var answerImageView: UIImageView!

    var correct: Bool = false {
        didSet {
            let imageName = self.correct ? "correct_image" : "incorrect_image"
            if let image = UIImage(named: imageName ) {
                self.answerImageView.image = image
            }
        }
    }

}

class MyTableViewCell: UITableViewController {

    @IBOutlet weak var collectionView: UICollectionView!

    struct Answer {
        let isCorrect: Bool
    }

    var answers = [Answer]() {
        didSet {
            self.tableView.reloadData()
        }
    }

    // MARK: - UITableViewDataSource

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if let cell = tableView.dequeueReusableCellWithIdentifier( "resultCell", forIndexPath: indexPath ) as? ResultTableViewCell {
            cell.correct = self.answers[ indexPath.row ].isCorrect
            return cell
        }

        fatalError( "Could not load cell" )
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.answers.count
    }
}

关于iOS-如何在 UITableview 中使用数组快速在一个原型(prototype) Cell 中显示 40 张图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693003/

相关文章:

ios - swift 3 : Custom Cell not displaying data

ios - 无法将我的 Swift 类访问到 Objective-C 应用程序中

java - 调用语音输入功能 - CodenameOne 应用程序(iOS 端口)

ios - 在 Swift 中为 mapView 上的每个注释显示不同的图像

ios6 - numberOfRowsInSection 不为自定义 tableviewcell、委托(delegate)/数据源集运行

ios - 快速获取错误的设备语言

swift - 使用 Swift 的 GeoFire

ios - 如何从数据源来自另一个 swift 文件的 tableView 激活 segue?

iphone - 如何将 UITableViewCell 导出到图像中?

ios - 设置 UILabel 顶部对齐在单元格中不起作用