2

我是 Swift 的新手,但是我以前在 Objective-C 工作过。如果单元格在 UITableView 中被重用,我在签入时遇到问题。

    let cell = tableView.dequeueReusableCellWithIdentifier(strCellId, forIndexPath:indexPath) as! MGSwipeTableCell
    cell.backgroundColor = UIColor.clearColor()

    let SMSObj = self.arraySMSContent[indexPath.row] as! SMSModel

    let lblMessage = UILabel(frame: CGRectMake(15, 10, Constants.SCREEN_WIDTH/1.4, Constants.SCREEN_HEIGHT/11))
    lblMessage.text = SMSObj.strSMSContent
    lblMessage.textAlignment = NSTextAlignment.Left
    lblMessage.numberOfLines = 2
    lblMessage.textColor = UIColor.whiteColor()
    cell.contentView.addSubview(lblMessage)

我用过 MGSwipebleCell。滚动 lblMessage 时重叠。即使我们无法检查 cell 是否为 nil。那么在这种情况下如何使用 viewWithTag 呢?纳克斯

4

2 回答 2

4

viewWithTag您可以使用单元重用标识符注册自定义类,而不是使用。然后您可以使用该子类的属性访问标签:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.registerClass(CustomCell.self, forCellReuseIdentifier: "CustomCell")
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell

    cell.smsLabel.text = ...

    return cell
}

在哪里:

class CustomCell: MGSwipeTableCell {
    var smsLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textAlignment = .Left
        label.numberOfLines = 2
        label.textColor = .whiteColor()

        return label
    }()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        backgroundColor = .blueColor()

        contentView.addSubview(smsLabel)
        NSLayoutConstraint.activateConstraints([
            smsLabel.topAnchor.constraintEqualToAnchor(contentView.topAnchor, constant: 5),
            smsLabel.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor, constant: -5),
            smsLabel.leadingAnchor.constraintEqualToAnchor(contentView.leadingAnchor, constant: 5),
            smsLabel.trailingAnchor.constraintEqualToAnchor(contentView.trailingAnchor, constant: -5)
        ])

        leftButtons = [MGSwipeButton(title: "Red", backgroundColor: .redColor())]
        leftSwipeSettings.transition = .Rotate3D;

        rightButtons = [MGSwipeButton(title: "Green", backgroundColor: .greenColor())]
        rightSwipeSettings.transition = .Rotate3D;
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        fatalError("not needed")
    }
}
于 2016-09-16T08:54:16.157 回答
1

有很多方法可以解决您的问题。请试试:

if let lblMessage = cell.contentView.viewWithTag(9999) as? UILabel { //Tag
    lblMessage.text = SMSObj.strSMSContent
}else{
    let lblMessage = UILabel(frame: CGRectMake(15, 10, Constants.SCREEN_WIDTH/1.4, Constants.SCREEN_HEIGHT/11))
    lblMessage.text = SMSObj.strSMSContent
    lblMessage.textAlignment = NSTextAlignment.Left
    lblMessage.numberOfLines = 2
    lblMessage.textColor = UIColor.whiteColor()
    lblMessage.tag = 9999 //Tag
    cell.contentView.addSubview(lblMessage)
}
于 2016-09-16T07:59:29.480 回答