【问题标题】:How can I assign a different UIImage in each UITableViewCell?如何在每个 UITableViewCell 中分配不同的 UIImage?
【发布时间】:2015-07-22 08:09:37
【问题描述】:

我正在努力弄清楚如何在每个 UITableViewCell 中显示不同的图像。

我目前在故事板的展开/折叠 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?.image = self.tableImages[indexPath.row] 怎么样?您需要在tableImages 中保留 8 张不同的图像。
  • 好的,它正在工作,但正在发生这种情况(为什么图片也在标题中)? i.imgur.com/0L14213.png。它们正确展开,但也将图像放在标题中。

标签: xcode swift uitableview uiimageview uiimage


【解决方案1】:

试试这个

    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
}

【讨论】:

  • var squatImage = UIImage(named:"First.png")! var squatImage2 = UIImage(named:"First.png")! 您为两个对象分配了相同的图像,也许这是个问题?
  • 好的,它正在工作,但正在发生这种情况(为什么图片也在标题中)? i.imgur.com/0L14213.png。它们可以正确展开,但也会将图像放在标题中。
  • 只在图像和标签之间创建水平空间约束,如果你不熟悉这个教程:raywenderlich.com/83130/…
  • 我觉得我解释得不是很好。那些小图像只是坐在每个标题中。当我点击单元格时,它会展开,一个大图像会显示在它应该出现的位置,但那个小图像也会覆盖在那里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多