简体   繁体   中英

UITableView Cell Label truncates on scrolling

i have a tableview which displays a custom created cell. The issue is that the label in my cell gets truncated everytime i scroll. Even if i give my label a big width it still truncates.

This is my cellForRowAt:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as? CoronaStatisticsCell else { return UITableViewCell() }

    let currentCountry = searchedCountry == nil ? countrySections[indexPath.section][indexPath.row] : searchedCountry![indexPath.row]
    cell.configure(country: currentCountry)
    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsets.zero
    cell.layoutMargins = UIEdgeInsets.zero

    return cell
}

This is my label which gets truncated in my cell:

private lazy var casesStaticticLbl: UILabel = {
    var lbl = UILabel()
    lbl.font = UIFont(name: "AvenirNext-Bold", size: 20)
    lbl.textAlignment = .left
    return lbl
}()

In the setSelected method i set my views:

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    layer.cornerRadius = 10
    countryLabel.translatesAutoresizingMaskIntoConstraints = false
    addSubview(countryLabel)

    NSLayoutConstraint.activate([
        countryLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10),
        countryLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),
        countryLabel.topAnchor.constraint(equalTo: topAnchor, constant: 10),
        countryLabel.heightAnchor.constraint(equalToConstant: 20)
    ])

    var casesSV = UIStackView(arrangedSubviews: [casesLbl, casesStaticticLbl])
    var deathsSV = UIStackView(arrangedSubviews: [deathLbl, deathStaticticLbl])
    var recoveredSV = UIStackView(arrangedSubviews: [recoveredLbl, recoveredStaticticLbl])

    for sv in [casesSV, deathsSV, recoveredSV] {
        sv.axis = .vertical
        sv.translatesAutoresizingMaskIntoConstraints = false
        sv.spacing = 10
        sv.alignment = .leading
        addSubview(sv)
    }

    NSLayoutConstraint.activate([
        casesSV.leadingAnchor.constraint(equalTo: countryLabel.leadingAnchor),
        casesSV.topAnchor.constraint(equalTo: countryLabel.bottomAnchor, constant: 5),
        casesSV.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20),

        deathsSV.centerXAnchor.constraint(equalTo: centerXAnchor),
        deathsSV.topAnchor.constraint(equalTo: countryLabel.bottomAnchor, constant: 5),
        deathsSV.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20),

        recoveredSV.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -30),
        recoveredSV.topAnchor.constraint(equalTo: countryLabel.bottomAnchor, constant: 5),
        recoveredSV.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20),
    ])
}

This is the initial label

在此处输入图片说明

after scrolling:

在此处输入图片说明

I think theres an issue with my stackviews because if i only add the label to the cell nothing truncates.

Thanks in advance

I'm answering your question based on the given contexts.

First off, Abhishek's comment is kinda agreeable. I would say you can definitely play with the constraints in the setSelected method, BUT not adding subviews. This merely comes from my personal preferences.

The only time you add subviews and set their constraints is in your init method override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) , but to reiterate, you can definitely toggle constraints in your setSelected AND also in your cellForRow .

To address your problem, given your current contexts in your question, you can add an explicit width as constraint. If you are stacking labels in a stackView, you must provide at least one explicit height or width constraint.


Lastly, take heed ⚠️ that Apple (App Store) and Google Play Store won't accept your app about

COVID-19 🦠

UNLESS you are from health organization. This is to prevent misinformation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM