【问题标题】:Tableview cell reusability, cutting label textsTableview 单元格可重用性,切割标签文本
【发布时间】:2017-11-30 08:31:50
【问题描述】:

Here I attached the gif file. Refer this

在我的表格视图中,我有一个带有自动布局的自定义 xib 单元格。当我运行项目时,遇到类似 gif 文件的问题。

这是我的表格视图功能

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "requestsCell", for: indexPath) as? RequestsTableViewCell
    cell?.backgroundColor = .clear

    let passengerName = cell?.viewWithTag(1) as! UILabel
    let bookingCreator = cell?.viewWithTag(2) as! UILabel
    let department = cell?.viewWithTag(3) as! UILabel
    let product = cell?.viewWithTag(4) as! UILabel
    passengerName.text = "Muhammed yasheed"
    bookingCreator.text = "Muhammed Yasheed"
    department.text = "Programming"
    product.text = "Flight"


    cell?.approvedView.isHidden = false
    cell?.cancelView.isHidden = true



    return cell!

} 

【问题讨论】:

  • 不相关,但表格视图单元格中的 viewWithTag 已过时。使用自定义类文件和插座。并强制解包类型转换为RequestsTableViewCell 以避免所有多余的问号,因为您最终还是强制解包单元格。
  • 我知道。我正在使用自定义类文件和插座。在这里,我只是检查了自定义类是否有任何问题。
  • 看起来像 xib/auto 布局问题。检查您的故事板以及您如何布置单元格。您可以为您的 UILabel 设置文本换行为 true 或 false。
  • @dirkgroten 只有两三个单元格是这样的。所有其他单元格工作正常。当我向下滚动并再次向上滚动时,单元格的标签变为正确的位置

标签: ios swift uitableview autolayout


【解决方案1】:

我终于找到了答案。只需在 cellForRowAt 索引路径中添加 cell?.layoutIfNeeded()

cell?.layoutIfNeeded()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多