// 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/