// Variable and ViewDidLoad
var auxRow = 0
var auxSection = 0
override func viewDidLoad() {
super.viewDidLoad()
}
//数量的部分中,只有一个部分为什么numberOfRowsInSection仅针对一个部分被多次调用?
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次。为什么会发生?
我不明白为什么在方法numberOfRowsInSection中输入4次。我只有一个部分 – PedroMeira
@Lamar:OP询问为什么numberOfRowsInSection方法执行4次,因为他只有1个部分(不是4个部分,所以理论上它应该只执行一次) –
numberOfRowsInSection方法执行4次,因为?是什么原因? – PedroMeira