Florian Pfisterer
Florian Pfisterer

Reputation: 1265

UITableView reusable custom cells show wrong content in subviews

I have a UITableViewController subclass in which I populate the UITableView with data as follows:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("kundeCell") as! KundeCell
    cell.selectionStyle = .None

    let kunde = self.kundenAuszutragen[indexPath.row]

    cell.delegate = self
    cell.kunde = kunde

    cell.allowsSwypeRight = (self.zahlungenImQuartalVonKunde[kunde] == nil) // Abkassieren
    cell.allowsSwypeLeft = self.state == .Austragen                         // Austragen

    cell.abkassierButton.titleAbkassierLabel.showsTextFilled = self.zahlungenImQuartalVonKunde[kunde] != nil

    return cell
}

This works fine, it looks like that: iOS Simulator with App running

The upper text is a UILabel, the bottom text is from a UIButton (in both cases the default ones). The € sign at the right is a custom UIButton with a subview of a custom UILabel that displays the € with a stroke effect, here's the relevant code of the custom UIButton, that is called when the button is initted:

private func handleInit()
{
    // titleAbkassierLabel is the custom UILabel with the stroke
    self.titleAbkassierLabel = AbkassierLabel(frame: CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
    self.titleAbkassierLabel.font = UIFont.systemFontOfSize(self.titleAbkassierLabelFontSize)
    self.titleAbkassierLabel.text = self.titleText
    self.titleAbkassierLabel.textColor = UIColor.clearColor()
    self.titleAbkassierLabel.setNeedsDisplay()
    self.addSubview(self.titleAbkassierLabel)

    self.backgroundColor = UIColor.clearColor()
}

Now when I set the € sign of a cell to be filled, it looks like that:my custom UITableViewCell with the € sign filled

The following code is then called in the UIButton subclass:

self.titleAbkassierLabel.showsTextFilled = true

// which then again comes to: (in the titleAbkassierLabel - property)
var showsTextFilled: Bool = true {  
    didSet
    {
        self.setNeedsDisplay()
    }
}

So okay, this cell is "highlighted" with the filled €. But if I then scroll down, other cells are highlighted as well (of course because the first cell is reused, by why does it show the right text (in the labels at the left) but not the right € sign? The cells that shouldn't be highlighted even look different from the others: compare it to the other cell, the € seems to be thicker (compare it to the other cell, the € seems to be thicker)

So it looks thicker, but if I look in ViewDebugger, I see that there's just one custom button and one custom € - label: enter image description here

If you need more information, just comment. Thank you very much and I hope you understand the problem despite the German property names/etc.!

EDIT: My Cell delegate:

protocol AustragenAbkassierenDelegate
{
    // Kunde bearbeiten
    func setKundeAusgetragen(kunde: Kunde, animated: Bool) -> Bool
    func setKundeAbkassiert(kunde: Kunde, createZahlung: Bool, endAction: (() -> Void)?) -> UIAlertController?

    func getNextKundeAfterKunde(kunde: Kunde) -> Kunde?

    func showKundeDetailVCWithKunde(kunde: Kunde)

    func showAlertController(alert: UIAlertController!, completionHandler: (() -> Void)?)
}

But I don't think this has something to do with the issue. It is just to notify the delegate (here the TableViewController) when something has changed.

Upvotes: 3

Views: 767

Answers (1)

Mike Taverne
Mike Taverne

Reputation: 9362

Try rebuilding the titleAbkassierLabel from scratch in your cell's prepareForReuse method.

Upvotes: 1

Related Questions