【问题标题】:How do I set the height for a custom UITableView?如何设置自定义 UITableView 的高度?
【发布时间】:2017-08-09 12:17:59
【问题描述】:

我将使用动态数量的单元格并在此基础上调整表格的高度。我可以通过我似乎无法调整表格大小来自己解决这个问题。无论我使用 100 个单元格还是 10 个单元格,视图都设置在相同的高度。

class generalTableViewController: UITableViewController {


    override func viewWillAppear(_ animated: Bool) {
        tableView.rowHeight = UITableViewAutomaticDimension
        // self.tableView.contentSize.height = 2000
        // self.tableView.frame.size.height = 2000
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1000
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        return cell
    }
}

【问题讨论】:

  • 使用-:tableView的heightForRowAtIndexPath方法
  • 您是否要设置UITableViewCell 高度?还是UITableView 高度?
  • 我可以调整 UITableViewCell,这是我遇到问题的 UITableView。

标签: ios swift uitableview height


【解决方案1】:

UITableViewControllers 通常接管整个视图。要使用 UITableView 仅占用视图的一部分,请使用 UIViewController。

如果要管理的视图由多个子视图组成,其中只有一个是表视图,则应使用 UIViewController 子类而不是 UITableViewController 的子类来管理表视图。 UITableViewController 类的默认行为是让表格视图填满导航栏和标签栏之间的屏幕(如果有的话)。

请参阅here 获取 Apple 指导。您基本上只需要处理一些控制器通常会为您处理的事情。

【讨论】:

  • 我尝试使用 UIViewController 但仍然无法绕过高度限制。我可能使用了错误的术语。当我滚动时,如果我说 1000 行,它只允许我滚动固定距离,所以如果我有更多行,我永远无法超越。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 2017-09-28
相关资源
最近更新 更多