Ios 确定单元格是否显示在屏幕上并执行功能
我想知道一个单元格是否以表格视图的形式显示在屏幕上 我尝试了Ios 确定单元格是否显示在屏幕上并执行功能,ios,uitableview,swift3,Ios,Uitableview,Swift3,我想知道一个单元格是否以表格视图的形式显示在屏幕上 我尝试了将显示方法,但没有用。我甚至试过 if (tableView.indexPathsForVisibleRows?.contains(indexPath))! { print("showing now") } 此功能可以工作,但当单元格在屏幕上或向下滚动时,它不会打印任何内容。例如:如果我有5个单元格,当应用程序启动并且单元格显示在屏幕上时,不会打印任何内容。另外,当我从单元格1滚动到单元格5时
将显示
方法,但没有用。我甚至试过
if (tableView.indexPathsForVisibleRows?.contains(indexPath))! {
print("showing now")
}
此功能可以工作,但当单元格在屏幕上或向下滚动时,它不会打印任何内容。例如:如果我有5个单元格,当应用程序启动并且单元格显示在屏幕上时,不会打印任何内容。另外,当我从单元格1滚动到单元格5时,不会显示任何内容。相反,如果我从第5单元向后滚动到第1单元,它将显示它,这完全违背了我的目的 我希望您理解我的疑问,并能帮助我找到合适的解决方案
干杯 记住连接
UITableView
的delegate
属性,并遵守UITableViewDelegate
试一试
来自
UITableViewDelegate
的协议。可以找到文档。将显示选项卡视图的方法应该适合您
正确地给出数据源
和委托
<代码>控制+拖动
从<代码>表格视图到黄色图标将显示正确的选项
将扩展添加到ViewControllerClass
并确认表协议-:
extension ViewController : UITableViewDelegate,UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}// Default is 1 if not implemented
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = indexPath.row
return cell!
}
// will display prints while displaying cells
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("visible now")
}
显示您尝试过的代码willDisplay应该可以工作。好吧,我之前调用过这个方法。这不太管用。不知怎么的,它现在开始工作了。另外,是否有一种方法,只有当“可见行”完全可见而不是部分可见时,我才能打印它。@AakashDave这将帮助您-:
extension ViewController : UITableViewDelegate,UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}// Default is 1 if not implemented
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = indexPath.row
return cell!
}
// will display prints while displaying cells
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("visible now")
}