【发布时间】:2018-05-12 01:19:46
【问题描述】:
我正在尝试通过检查“willDisplay 单元格”处的索引路径来在 UITableView 中实现无限滚动,但是一旦表格加载了数据,“willDisplay 单元格”函数似乎在我向下滚动之前绘制所有单元格,尽管手机屏幕一次只能容纳一两个单元格。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print(indexPath.row)
}
无需向下滚动即可输出:
0
1
2
3
4
5
当我向下滚动时,“willDisplay cell”函数正常工作,并且一次打印一个 indexpath.row。
这可能是什么问题?
下面是完整代码:
override func viewDidLoad() {
super.viewDidLoad()
postsTableview.delegate = self
postsTableview.dataSource = self
let nibName = UINib(nibName: "postTableViewCell", bundle:nil)
postsTableview.register(nibName, forCellReuseIdentifier: "postTableViewCell")
loadPosts()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postTableViewCell", for: indexPath) as! postTableViewCell
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print(indexPath.row)
}
我已经尝试将tableview单元格高度设置为超过屏幕高度的较大值,但我仍然遇到同样的问题
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return 1000.0;
}
【问题讨论】:
-
“同时绘制所有单元格”是什么意思?每个单元格都是单独绘制的。
-
您期望的行为是什么?在我看来,当屏幕加载时,你的表格视图有空间显示 5 个单元格,所以你会得到你提到的控制台输出。
-
没有屏幕只能放一个单元格
-
这里看看屏幕如何一次只能容纳一个单元格imgur.com/a/o1VJ9
-
@holex 如果每个单元格在即将出现在屏幕上时都是单独绘制的,为什么控制台输出会同时显示所有索引路径编号而不向下滚动,而屏幕尺寸只能适合单元格
标签: ios swift uitableview