【问题标题】:Determine if a cell in on screen and perform a function确定屏幕上是否有单元格并执行功能
【发布时间】:2017-08-20 07:59:31
【问题描述】:

我想知道一个单元格是否在表格视图中显示在屏幕上。

我尝试了willDisplay 方法,但没有用。我什至尝试过

if (tableView.indexPathsForVisibleRows?.contains(indexPath))! {
                    print("showing now")
}


此功能有效,但是当单元格在屏幕上或向下滚动时,它不会打印任何内容。例如:如果我有 5 个单元格,当应用程序启动并且单元格在显示屏上时,没有打印任何内容。此外,当我从单元格 1 滚动到 5 时,没有显示任何内容。相反,如果我从单元格 5 向后滚动到 1,它将显示它,这完全违背了我的目的。

希望您能理解我的疑问,并能帮助我找到合适的解决方案。

干杯!

【问题讨论】:

  • 显示您尝试过的代码 willDisplay 应该可以工作。

标签: ios uitableview swift3


【解决方案1】:

willDisplay tabView 方法应该适合你。

正确地给DataSourceDelegateControl+dragTableView 到黄色图标将显示正确的选项。

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")
    }

【讨论】:

  • 好吧,我之前调用过这个方法。它并不完全奏效。它现在以某种方式工作。
  • 另外,有没有一种方法可以打印“可见行”,只有当它完全可见而不是部分可见时。
  • @AakashDave 这会帮助你-:stackoverflow.com/questions/3326658/…
【解决方案2】:

记得连接UITableViewdelegate属性并符合UITableViewDelegate

试试

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    print("cell \(indexPath.row) is about to display")
}

来自UITableViewDelegate 的协议。可以在here 找到文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-15
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多