【问题标题】:TableView only loading contents of one cell as I scroll当我滚动时,TableView 只加载一个单元格的内容
【发布时间】:2016-11-25 04:46:50
【问题描述】:

我正在将 UIViews 加载到我的 tableview 单元格内容视图中,但是当我滚动浏览我的 tableview 时,只有一个单元格加载了它的视图。它为每个单元格加载正确的视图,但它只加载一个,然后随着新单元格从底部出现而消失。所有数据都通过函数 makeTableViewRowView(填充 uiview 的字符串数组)在本地加载。

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: expenseCell, for: indexPath)
        tableViewRow = makeTableViewRowView(indexPath: indexPath)
        tableViewRow.translatesAutoresizingMaskIntoConstraints = false
        cell.contentView.addSubview(tableViewRow)
        cell.selectionStyle = .none

        let margins = cell.contentView.layoutMarginsGuide
        tableViewRow.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true
        tableViewRow.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        tableViewRow.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
        tableViewRow.heightAnchor.constraint(equalToConstant: 40).isActive = true
        return cell
    }

【问题讨论】:

  • 什么是tableViewRow
  • @Mr.Bista 这只是一个持有 UIView 的属性导致 makeTableViewRowView 返回一个 UIView
  • 因为tableViewRow 只初始化一次,所以它只出现在最后一个单元格中
  • 尝试使用自定义单元格,而不是在cellForRowAt中添加子视图
  • @HamzaAnsari 是的。做到了。

标签: ios swift uitableview uiview


【解决方案1】:

在界面生成器中创建自定义单元格并deque 它。并根据cellForRowAtIndexPath 中该单元格的要求进行更改!因为cellForRowAtIndexPath 将在您的单元格为deque 时被调用,我的意思是单元格将被重用!因此,在cellforrow 中创建视图并将其添加为子视图不是很好的解决方案,您可以直接从界面生成器中添加,并可以根据需要在cellforrow 中进行操作!

【讨论】:

  • 好主意,但我没有在这个项目中使用 IB。 Hamza 的回答对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
相关资源
最近更新 更多