我很努力地弄清楚,我可以在每个UITableViewCell中显示不同的图像。如何在每个UITableViewCell中分配不同的UIImage?
我目前在我的故事板中展开/折叠UITableViewCell中有一个UIImageView。我可以通过在这个UIImageView的属性中定义图像来为它们分配一个图像,但是它显示了每个单元格中的相同图像(显然)。
这是我对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:
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()
}
}
}
怎么样'cell.imageView =图像配? self.tableImages [indexPath.row]'?你需要在'tableImages'中保留8个不同的图像。 –
好吧,它正在工作,但这是发生(为什么图片也在标题中)? http://i.imgur.com/0L14213.png。它们正确地展开,但它也将标题形象化。 – DanTdd