Ios 为什么只为一个区段多次调用numberOfRowsInSection?

Ios 为什么只为一个区段多次调用numberOfRowsInSection?,ios,arrays,swift,uitableview,tableview,Ios,Arrays,Swift,Uitableview,Tableview,//节数,仅一节 // Variable and ViewDidLoad var auxRow = 0 var auxSection = 0 override func viewDidLoad() { super.viewDidLoad() } //行数 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { auxSection += 1 p

//节数,仅一节

// Variable and ViewDidLoad

var auxRow = 0
var auxSection = 0   

override func viewDidLoad() {
    super.viewDidLoad()
}
//行数

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        auxSection += 1
        print("times section: ", auxSection)
        return 1
    }
//TableView配置单元格

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    auxRow += 1
    print("times row: ", auxRow)
    return 2
}
输出:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let textCellIdentifier = "cellText"
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
    let variable : String = indexPath.row.description
    cell.textLabel?.text = variable
    return cell

}

方法
numberOfRowsInSection
被调用了4次。为什么会发生这种情况?

苹果不保证UIKit调用您的委托方法的频率。他们拥有这个框架。我们可以期望他们缓存该值,但没有什么要求他们这样做。

您可以在
numberofrowsinssection
方法中设置断点,以查看它在内部的用途。没有理由不多次调用它,因为在tableview内部需要知道它有多少行的任何时候都会调用它。它被调用了4次,因为Apple的tableview代码需要在4个不同的时间知道一个分区中有多少行

我不明白为什么在方法numberofrowsin部分输入4次。我只有一个section@Lamar:OP询问numberOfRowsInSection方法为什么执行4次,因为他只有1个部分(不是4个部分,所以理论上该方法只能执行一次)numberOfRowsInSection方法执行4次,因为?原因是什么?重要吗?这会给你带来麻烦吗?只有了解Apple源代码的人才能告诉您……此
numberofrowsinssection
方法将返回tableview的行数。。因为数据结构中有4个对象。它将返回4行。调用4次numberOfRowsInSection和numberOfSectionsInTableView->times section:1次行:1次段:2次行:2次段:3次行:3次段:4次行:4@PedroMeira我很抱歉,但我不明白这意味着什么,这两种方法都可以调用任意数量的时间取决于tableview在内部使用它们的方式。它与tableview的行数或节数没有100%的联系。我只有一个节,方法numberOfRowsInSection应该只调用一次,为什么要调用4次?我更改了问题代码,方法numberOfSectionsInTableView被调用了4次。原因是什么?我不明白为什么会发生这种情况,我想看看是不是我做错了什么“我只有一个部分,方法numberOfRowsInSection应该只调用一次”是不正确的。你的基本假设是正确的,这是错误的。正确的做法是“我只有一个部分,方法numberOfRowsInSection的调用次数应与Apple的tableview代码调用次数相同,在我的例子中,调用次数为4。”
    times section:  1
    times row:  1
    times section:  2
    times row:  2
    times section:  3
    times row:  3
    times section:  4
    times row:  4