Can't reset UILabel attributedText when a UITableViewCell is reused

前端 未结 2 1045
半阙折子戏
半阙折子戏 2020-12-31 16:48

The problem

I\'m using a UITableView to show the list of transactions of a credit card. If the transaction is a chargeback, I\'m adding a strikethroug

相关标签:
2条回答
  • 2020-12-31 17:48

    You should try to set with .attributedText here instead of using `.text'. If it won't work I'll delete my answer.

    // Fill in the values
    transactionDescriptionLabel.text = transactionData.description
    transactionValueLabel.text = Formatter.currency.string(for: transactionData.balance)
    

    So, try this

    transactionDescriptionLabel.attributedText = //
    transactionValueLabel.attributedText = //
    

    One more thing. Actually I don't like didSet. I suggest you to create a method to configure your cell. Here is an example of what I want to tell you.

    func configure(with transactionData: TimelineResponseModel) {
       // Reset the labels
       transactionDescriptionLabel.text = nil
       transactionDescriptionLabel.attributedText = nil
       transactionValueLabel.text = nil
       transactionValueLabel.attributedText = nil
    
       // Fill in the values
       transactionDescriptionLabel.text = transactionData.description
       transactionValueLabel.text = Formatter.currency.string(for: transactionData.balance)
    
       if transactionData.isChargeback {
          let value = Formatter.currency.string(for: transactionData.balance) ?? ""
          transactionDescriptionLabel.attributedText = transactionData.description.strikedThrough()
          transactionValueLabel.attributedText = value.strikedThrough()
       }
    }
    

    Next.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: TimelineTableViewCell.reuseIdentifier,
                                                       for: indexPath) as? TimelineTableViewCell else { fatalError() }
        // Thats what I really like
        cell.configure(with: viewModel.timeline[indexPath.row])
        return cell
    }
    
    0 讨论(0)
  • 2020-12-31 17:50

    Alex answer did fix my problem, but I've also found out why the reset of the labels didn't work. I'll leave it here in case it's useful for someone.

    For some reason, if I reset only the attributedText, it works:

    transactionDescriptionLabel.attributedText = nil
    transactionValueLabel.attributedText = nil
    

    Or, if I reset the attributedText first, and then reset the text, it also works:

    transactionDescriptionLabel.attributedText = nil
    transactionValueLabel.attributedText = nil
    transactionDescriptionLabel.text = nil
    transactionValueLabel.text = nil
    

    According to the UILabel documentation, assigning a new value to either property replaces the value of the other, so the order should not matter. But it does, apparently.

    https://developer.apple.com/documentation/uikit/uilabel/1620538-text

    https://developer.apple.com/documentation/uikit/uilabel/1620542-attributedtext

    0 讨论(0)
提交回复
热议问题