ios - 为什么 numberOfRowsInSection 仅针对一个部分被多次调用?

标签 ios arrays swift uitableview tableview

// Variable and ViewDidLoad

var auxRow = 0
var auxSection = 0   

override func viewDidLoad() {
    super.viewDidLoad()
}

//Number Of Sections,只有一个section

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        auxSection += 1
        print("times section: ", auxSection)
        return 1
    }

//行数

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

//TableView配置单元格

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

}

输出:

    times section:  1
    times row:  1
    times section:  2
    times row:  2
    times section:  3
    times row:  3
    times section:  4
    times row:  4

numberOfRowsInSection 方法被调用了 4 次。为什么会这样?

最佳答案

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

关于ios - 为什么 numberOfRowsInSection 仅针对一个部分被多次调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36224842/

相关文章:

iOS Kiwi + Nocilla - 未能 stub 请求

ios - Obj-C - 如何保护此排序数组免受空值影响

ios - 呈现模态视图 Controller 时,核心动画层表现得很奇怪。 CATransform3D 问题

android - 在 Android 和 IOS 中,我可以在不打开邮件应用程序的情况下使用 IONIC 发送邮件吗?

java - 使用嵌套 for 循环修改二维数组

ios - 如何快速转换java代码?

ios - 核心数据一对多 Swift

ios - 如何交互式收缩和增长 Table View Cell?

ios - AVAudioSession:在应用程序中录制音频,同时允许“音乐”应用程序通过蓝牙播放

java - Java 中的井字游戏