简体   繁体   English

iOS-如何使用数组快速在UITableview中的一个原型单元中显示40张图像?

[英]iOS-How to display 40 images in one prototype Cell inside UITableview using array in swift?

我正在创建在线教程应用程序,其中有40个问题。我想使用绿色图像显示正确的问题,使用红色图像显示错误的答案来显示问题的结果。我需要在单个原型单元中显示这40个图像。我想迅速知道。

A table view is designed to show multiple items in a scrolling list, not just one item. 表格视图旨在在滚动列表中显示多个项目,而不仅仅是一个项目。 It sounds more like you want to have a table view with 40 rows, each of which displays the answer given for the question. 听起来更像是要具有40行的表格视图,每行都显示针对该问题的答案。 You could set that up easily like so: 您可以像这样轻松设置:

// 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM