xcode - 如何在每个 UITableViewCell 中分配不同的 UIImage?

标签 xcode swift uitableview uiimageview uiimage

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

我目前在 Storyboard的展开/折叠 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()
        }
    }
}

最佳答案

试试这个

    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
}

关于xcode - 如何在每个 UITableViewCell 中分配不同的 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31557420/

相关文章:

xcode - 无法从 xcode 中的提交中排除用户界面状态

iOS 谷歌地图方法 UIView 动画不起作用

ios - 在 XCode 中进行 UI 测试期间无法访问自定义 View

ios - 任何想法如何解决这个奇怪的 uitableview 分隔线格式问题

ios - 静态单元格的内容未出现在 UITableView 中

objective-c - Xcode 4.3 停止构建 XIB 文件

ios - 将 BuddySDK 平台添加到包含 RestKit 的现有项目

ios - 为什么Xcode没有ios框架选项?

swift - applicationWillHide 未调用

ios - UITableView 子类初始化设置不起作用