iOS-如何在一个原型中使用swift中的数组在UITableview中显示40个图像?

时间:2015-07-29 06:39:48

标签: ios swift uitableview

我正在创建在线教程app.In我有40个问题。我想显示问题的结果使用绿色图像正确和红色图像的错误答案。我需要在单个内显示这40个图像原型单元。我想在swift中知道。

1 个答案:

答案 0 :(得分:0)

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

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