【发布时间】: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