ios - UITableView 单元格标签在滚动时截断

标签 ios swift xcode uitableview uikit

我有一个表格 View ,它显示一个自定义创建的单元格。问题是每次滚动时我的单元格中的标签都会被截断。即使我给我的标签一个很大的宽度,它仍然会被截断。

这是我的 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
}

这是我的标签,它在我的单元格中被截断:

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

在 setSelected 方法中,我设置了我的 View :

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),
    ])
}

这是初始标签

enter image description here

滚动后:

enter image description here

我认为我的堆栈 View 存在问题,因为如果我只将标签添加到单元格,则不会截断任何内容。

提前致谢

最佳答案

我正在根据给定的上下文回答您的问题。

首先,Abhishek 的评论有点令人愉快。我想说你绝对可以使用 setSelected 中的约束。方法,但不添加 subview 。这仅来自我的个人喜好。

您添加 subview 并设置其约束的唯一时间是在您的 init 方法 override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) ,但重申一下,您绝对可以在 setSelected 中切换约束。也可以在您的 cellForRow 中.

为了解决您的问题,鉴于您当前问题中的上下文,您可以添加 显式宽度 作为约束。如果您在 stackView 中堆叠标签,则必须提供至少一个明确的高度或宽度约束。

最后,请注意⚠️ Apple (App Store) 和 Google Play Store 不会接受您的应用

COVID-19 🦠

除非您来自卫生组织。这是为了防止错误信息。

关于ios - UITableView 单元格标签在滚动时截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60767116/

相关文章:

ios - 从 XIB 加载时 UICollectionViewCell 导致崩溃

javascript - Safari Web 扩展无法发送 native 消息

ios - Typhoon 依赖注入(inject)和 Swift 3 : Appdelegate isn't AnyObject

iphone - 为什么只有 viewWillAppear 在返回导航时被调用

ios - Swift:UITableView - 滑动以删除 - 如何在滑动时让单元格内容不移动

ios - 如何在 Swift 完成外部异步请求之前先完成内部异步请求?

iphone - UILongPressGestureRecognizer - 只识别最后一次按下

objective-c - iPhone :Store & Retrieve NSMutableArray object

ios - 如何使用 CoreData 和 CloudKit 处理离线模式?

xcode - PrepareForSegue Swift(更改 UIImage 和 UILabel)