【问题标题】:Why does my tableView function run three times?为什么我的 tableView 函数运行了 3 次?
【发布时间】:2015-01-26 04:28:24
【问题描述】:

我有一个 UITableView,并且我有执行它所需的方法之一:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    println("section is \(section)")
    println(self.items.count) // self.items.count = 3
    return self.items.count
}

在我的日志中,我看到该方法运行了 3 次:

section is 0
3
section is 0
3
section is 0
3

编辑:

这是我部分的sn-p:

func numberOfSectionsInTableView(tableView:UITableView!)->Int
{
    return 1
}

我的线程栈图片:http://i.imgur.com/90dakCu.png

【问题讨论】:

  • 设置断点并查看每次调用的原因/时间。
  • 你有多个部分吗?
  • 我只有 1 个。我在原始帖子的编辑部分添加了该代码的 sn-p。当我在这两个人上设置断点时,它首先运行 numberOfSectionsInTableView(),然后运行 ​​tableView(),它执行了 3 次该过程。
  • @DougWatkins 他最好不要,因为结果是基于单个数组的。
  • 你在哪里调用tableView上的reloadData?每次调用时,每个部分都会调用该方法。

标签: ios uitableview swift xcode6.1.1


【解决方案1】:

在 UITableView 中有很多情况,由于自动刷新 tableview 之类的原因,不同的委托方法会被多次调用。

Check this answer for more informations.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 2013-11-24
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多