我正在创建在线教程应用程序。我有 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/