如何在每个 UITableViewCell 中分配不同的 UIImage? [英] How can I assign a different UIImage in each UITableViewCell?
本文介绍了如何在每个 UITableViewCell 中分配不同的 UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力弄清楚如何在每个 UITableViewCell 中显示不同的图像.
I am struggling to figure out how exactly I can display a different image in each UITableViewCell.
我目前在故事板的展开/折叠 UITableViewCell 中有一个 UIImageView.我可以通过在此 UIImageView 的属性中定义图像来为它们分配一个图像,但在每个单元格中显示相同的图像(显然).
I currently have a UIImageView in an expanding / collapsing UITableViewCell in my storyboard. I can assign an image to them all by defining the image in the attributes for this UIImageView but that shows the same image in each cell (obviously).
这是我为 TableViewController 编写的代码:
This is the code I have for TableViewController:
import UIKit
let cellID = "cell"
class TableViewController: UITableViewController {
var selectedIndexPath : NSIndexPath?
var tableData = ["Squats","Bench Press","Bent Over Row","Barbell Shrugs","Tricep Extensions","Straight Bar","Hyperextensions","Cable Crunches"]
var squatImage = UIImage(named:"First.png")!
var squatImage2 = UIImage(named:"First.png")!
var tableImages: [UIImage] = []
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! PickerTableViewCell
cell.titleLabel?.text = self.tableData[indexPath.row]
// cell.imageView?.image = self.tableImages[0]
// **This does NOT work**, it assigns a picture into each cell and on each cells title.
return cell
}
override func viewDidLoad() {
tableImages = [squatImage,squatImage2]
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let previousIndexPath = selectedIndexPath
if indexPath == selectedIndexPath {
selectedIndexPath = nil
} else {
selectedIndexPath = indexPath
}
var indexPaths : Array<NSIndexPath> = []
if let previous = previousIndexPath {
indexPaths += [previous]
}
if let current = selectedIndexPath {
indexPaths += [current]
}
if indexPaths.count > 0 {
tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
(cell as! PickerTableViewCell).watchFrameChanges()
}
override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
(cell as! PickerTableViewCell).ignoreFrameChanges()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath == selectedIndexPath {
return PickerTableViewCell.expandedHeight
} else {
return PickerTableViewCell.defaultHeight
}
}
}
对于 PickerTableViewCell:
and for PickerTableViewCell:
import UIKit
class PickerTableViewCell : UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
class var expandedHeight: CGFloat { get { return 200 } }
class var defaultHeight: CGFloat { get { return 44 } }
@IBOutlet weak var squatImage: UIImageView!
func checkHeight() {
squatImage.hidden = (frame.size.height < PickerTableViewCell.expandedHeight)
}
func watchFrameChanges() {
addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions.New|NSKeyValueObservingOptions.Initial, context: nil)
}
func ignoreFrameChanges() {
removeObserver(self, forKeyPath: "frame")
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if keyPath == "frame" {
checkHeight()
}
}
}
推荐答案
试试这个
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
cell.titleLabel?.text = self.tableData[indexPath.row]
cell.imageView?.image = self.tableImages[indexPath.row % 2]
return cell
}
这篇关于如何在每个 UITableViewCell 中分配不同的 UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文