2

I'm new to Swift, however I work in Objective-C before. I'm having issue in checkin if the cell is being reused in UITableView or not.

    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)

I have used MGSwipebleCell. While scrolling lblMessage overlaps. Even we can not check if cell is nil or not. So how to use viewWithTag in this situation? Thnaks

Hiren Prajapati
  • 717
  • 3
  • 10
  • 25

2 Answers2

4

Rather than using viewWithTag, you can register a custom class with the cell reuse identifier. Then you can access the label using a property of that subclass:

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
}

Where:

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")
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks for the answer. I know to create custom class in Swift but I also have to use MGSwipeTableCell (third party library). So I don't know how to do this – Hiren Prajapati Sep 16 '16 at 09:31
  • 1
    If you notice, my custom class _is_ a subclass of `MGSwipeTableCell`. I even set left and right buttons. – Rob Sep 16 '16 at 09:34
  • Thnak you guys. Finally figured it out. Though @Rob, the way you have declared SMSLabel in custom class is out of my mind. I usually declare objects outside functions and I setup frame and properties of objects in "init" method of custom cell. Can you tell me the difference? – Hiren Prajapati Sep 16 '16 at 10:13
  • Are you talking about my initializing `smsLabel` with a closure? See [Setting a Default Property Value with a Closure or Function](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID232). It's a nice way to encapsulate details related to the configuration of an individual property. It's particularly nice when `init` starts getting long. – Rob Sep 16 '16 at 10:54
1

There're many ways to solve your problem. Please try:

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)
}
Quang Hà
  • 4,613
  • 3
  • 24
  • 40
  • Thank you. Thats what I needed. But it throws a compile error: VALUE OF TYPE 'UIView' HAS NO MEMBER 'TEXT' (in your second line) – Hiren Prajapati Sep 16 '16 at 09:39