【问题标题】:UITableView Cell Label truncates on scrollingUITableView 单元格标签在滚动时截断
【发布时间】:2020-03-20 00:50:00
【问题描述】:

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

这是我的 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 方法中,我设置了我的视图:

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

这是初始标签

滚动后:

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

提前致谢

【问题讨论】:

  • 不要在 setSelected 中使用约束...您也可以发布整个单元格代码...吗?

标签: ios swift xcode uitableview uikit


【解决方案1】:

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

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

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

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


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

COVID-19 ?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多