【问题标题】:Populate a Prototype Cell by Passing Object通过传递对象填充原型单元
【发布时间】:2015-05-14 17:04:02
【问题描述】:

我有一个 UITableView 正在使用原型单元(故事板中的所有内容)。理想情况下,我想通过 cellForRowAtIndexPath 向这些单元格传递一个对象,并让单元格弄清楚如何填充它的标签等...

setSelected 似乎有效,但前提是单元格未滚动到屏幕外。一旦它们从屏幕上滚出,单元格的大部分逻辑就会被破坏。查看当前方法的示例代码:

class RequirementsAndContentsCell: UITableViewCell {

    var represents = String()
    var item = PLItem()
    var requirement = PLCaseRequirement()
    var content = PLCaseContent()

    @IBOutlet weak var itemNameLabel: UILabel!
    @IBOutlet weak var tagView: UIView!
    @IBOutlet weak var tagLabel: UILabel!
    @IBOutlet weak var barcodeIcon: UIImageView!
    @IBOutlet weak var barcodeLabel: UILabel!
    @IBOutlet weak var unitCountLabel: UILabel!
    @IBOutlet weak var statusView: UIView!
    @IBOutlet weak var mainLabelVerticalAlign: NSLayoutConstraint!
    @IBOutlet weak var barcodeIconLeadingSpace: NSLayoutConstraint!
    @IBOutlet weak var tagViewLeadingSpace: NSLayoutConstraint!


    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        tagView.layer.cornerRadius = 5

        if represents == "content" {
            itemNameLabel.text = content.name
            unitCountLabel.attributedText = createAttributedString("\(content.qty)", "\nUnits", "HelveticaNeue-Light", 12, UIColor.darkTextColor())
            barcodeLabel.text = content.barcode
            statusView.hidden = true
            if content.isParent == true {
                tagView.hidden = true
                tagViewLeadingSpace.priority = 1
            }
        } else if represents == "requirement" {
            itemNameLabel.text = requirement.name
            mainLabelVerticalAlign.constant = 0
            tagView.hidden = true
            barcodeIcon.hidden = true
            barcodeLabel.hidden = true
            unitCountLabel.attributedText = createAttributedString("\(requirement.qty)", "\nUnits", "HelveticaNeue-Light", 12, UIColor.darkTextColor())
            if item.isContainer == true {
                updateStatusView()
            } else {
                statusView.hidden = true
            }

        }


    }



    func updateStatusView () {
        if requirement.fulfillmentLevel == 0 {
            statusView.layer.backgroundColor = UIColor.redColor().CGColor
        } else if requirement.fulfillmentLevel == 1 {
            statusView.layer.backgroundColor = GlobalVars.yellowColor.CGColor
        } else if requirement.fulfillmentLevel == 2 {
            statusView.layer.backgroundColor = GlobalVars.goodColor.CGColor
        } else {
            statusView.layer.backgroundColor = UIColor.orangeColor().CGColor
        }
    }

}

达到预期结果的正确方法是什么?

【问题讨论】:

    标签: ios uitableview swift


    【解决方案1】:

    您不应该尝试填充setSelected 中的单元格。创建一个方法,将您要绑定的对象作为参数传递,然后从 cellForRowAtIndexPath 调用它。

    【讨论】:

      【解决方案2】:

      您可以实现UITableViewDelegate的函数func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath),以便在单元格即将显示时更新其视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-13
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-14
        相关资源
        最近更新 更多