Стиль содержимого в TableView без задержки прокрутки - PullRequest
0 голосов
/ 18 марта 2020

У меня есть TableView, с 15-20 отображаемыми ячейками. У ячейки есть Метка со следующим классом:

import Foundation
import UIKit

class RoundedLabel: UILabel {

    var inset: CGFloat = 16

    override func drawText(in rect: CGRect) {
        let insets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
        super.drawText(in: rect.inset(by: insets))
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        updateCornerRadius()
    }


    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(
            width: size.width + inset,
            height: size.height
        )
    }

    @IBInspectable var rounded: Bool = false {
        didSet {
            updateCornerRadius()
        }
    }

    func updateCornerRadius() {
        layer.cornerRadius = rounded ? frame.size.height / 2 : 0
        layer.masksToBounds = true
    }
}

В моем V C у меня есть следующее (у меня есть TableViews в одном V C):


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if tableView == tableViewMainLatest {
        let cell = tableViewMainLatest.dequeueReusableCell(withIdentifier: "cellMainLatest", for: indexPath) as! MainLatestTableViewCell

        let postDate = posts[indexPath.row].postDate ?? 0
        let formattedPostDate = Date(timeIntervalSince1970: postDate)
        let timeAgo = formattedPostDate.timeAgo(numericDates: false)

        let postCity = posts[indexPath.row].postFromCity ?? "?"
        let postCountry = posts[indexPath.row].postFromCountry ?? "?"
        let postComments = posts[indexPath.row].comments ?? 0

        let maxPostComments: String?

        // Only show the number if comment amount is less than 100
        if postComments > 99 {
            maxPostComments = "99+"
        } else {
            maxPostComments = String(postComments)
        }

        cell.labelPostDate.text = timeAgo
        cell.labelPostFrom.text = postCity + ", " + postCountry
        cell.labelAmountComments.text = maxPostComments

        return cell

    } else if tableView == tableViewCategories {
        let cell = tableViewCategories.dequeueReusableCell(withIdentifier: "cellMainCategories", for: indexPath) as! MainApplicationCategoriesTableViewCell
        cell.labelCategories.text = pseudo[indexPath.row] as? String

        cell.alpha = 0.55

        UIView.animate(withDuration: 0.5, animations: {
            cell.alpha = 1
        })

        return cell
    }

    return UITableViewCell()

}

Моя CustomCell:

class MainApplicationCategoriesTableViewCell: UITableViewCell {

    // Storyboard
    @IBOutlet weak var labelCategories: UILabel! {
        didSet {
            labelCategories.layer.borderWidth = 1
            labelCategories.layer.borderColor = UIColor(red:0.88, green:0.88, blue:0.88, alpha:1.0).cgColor
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.selectionStyle = .none
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
}

Когда я использую этот фрагмент кода:

// Storyboard
@IBOutlet weak var labelCategories: UILabel! {
    didSet {
        labelCategories.layer.borderWidth = 1
        labelCategories.layer.borderColor = UIColor(red:0.88, green:0.88, blue:0.88, alpha:1.0).cgColor
    }
}

Прокрутка TableView запаздывает. Где я должен применить borderWidth / borderColor, чтобы все еще иметь хороший пользовательский опыт без отставания?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...