【问题标题】:TableView empties when interactingTableView 交互时清空
【发布时间】:2020-02-20 21:49:25
【问题描述】:

所以我有 2 个视图显示在 UIView 中,这取决于在 SegmentViewController 上选择的内容。我创建虚拟数据,返回 20 行自定义单元格。这很好用。

一切都很好,直到我与 TableView 交互。

下面是我的代码:

GoalsViewController.swift

import UIKit

class GoalsViewController: UIViewController {

    @IBOutlet weak var goalsTableView: UITableView!
    let goalCellIdentifier = "goalCell"


    override func viewDidLoad() {
        super.viewDidLoad()

        goalsTableView.delegate = self
        goalsTableView.dataSource = self

        goalsTableView.register(UINib(nibName: "GoalsViewCell", bundle: nil), forCellReuseIdentifier: goalCellIdentifier)

        goalsTableView.reloadData()
    }


}

extension GoalsViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = goalsTableView.dequeueReusableCell(withIdentifier: goalCellIdentifier, for: indexPath) as! GoalsViewCell
        cell.goalTitle.text = "aaaaa"
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Selected \(indexPath.row)")
    }

   }

选择任何空行后,不会调用didSelectRowAt,因此单元格根本不存在。我试图找到一个解决方案,但我只是在被填充之前找到关于空列表的问题。

空的tableview可能是什么原因?

【问题讨论】:

  • 你在其他地方打电话给reloadData吗?
  • 看起来您包含的视图没有正确约束,因此一旦有自动布局通道,它们就会被调整大小或移出屏幕。您是否在控制台中收到约束警告?为什么显示“调试视图层次结构”?尝试“揭示”。这是一个很棒的工具
  • @dfd 不,我不会在其他地方称呼它。甚至不在主 ViewController 上。 @Paulw11 这是一个很棒的工具。谢谢你的建议,不知道!因此,我将文本名称更改为indexPath.row 以了解哪一行。当列表已满时,我可以用白色看到可见的项目。单击后,所有内容都变为灰色,但值仍然存在。这是截图i.imgur.com/OjDYjqY.png

标签: ios swift xcode uitableview


【解决方案1】:

我在这里可能错了,但我注意到的一件事是您没有实现设置每个单元格高度的函数。

// Specify the height of your cells
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   return 100 // Or your given cell height.
}

所以这是我的理论:如果您使用约束,则缺少某些东西,并且您的单元格的高度不能单独通过约束来识别,或者您根本没有使用约束,因此您必须使用 heightForRowAt 函数来指定每个单元格的高度.

【讨论】:

  • 不幸的是它没有帮助。我还尝试通过在表格上设置 rowHeight 属性,直接在 viewDidLoad 函数上进行设置。大小变为我设置的大小,但它又是空的。
【解决方案2】:

我将为可能不知道的人(比如我)解释问题是什么。 所以我的UITableViewUIView 内,它会根据用户的选择而变化。总的来说,我有 2 个不同的视图可以切换。它清空它的原因是因为我的父 ViewController 无法访问 UITableView 的委托。要解决这个问题,在向 UIView 添加子视图后,您还需要将 ViewController 移动到父控制器。在代码中是这样的。

// Empty array of UIViewControllers
var views: [UIViewController]!

// Add the UIViewControllers to the array
views = [UIViewController()]
views.append(EventsViewController())
views.append(GoalsViewController())

for view in views {
      // Needed to adjust the size of Subview to size of View
      view.view.frame = containerView.bounds
      // Add the subviews
      containerView.addSubview(view.view)
}

// Bring the view in position 1 to the front of the UIView
containerView.bringSubviewToFront(views[1].view)
// Add Views[1] UIViewController as a child to the parent controller
self.addChild(views[1])
views[1].didMove(toParent: self)

// After done with everything with the UIViewController remove it
views[1].removeFromParent()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多