【发布时间】:2016-07-13 11:58:22
【问题描述】:
// 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
}
//行数
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 次。我只有一节
-
@Lamar:OP 在问为什么 numberOfRowsInSection 方法执行 4 次,因为他只有 1 个部分(不是 4 个部分,所以理论上该方法应该只执行一次)
-
numberOfRowsInSection 方法执行 4 次,因为?是什么原因?
-
这有关系吗?它给你带来了问题吗?只有了解 Apple 源代码的人才能真正告诉您……
-
此
numberOfRowsInSection方法将返回您的tableview 的行数。因为您的数据结构中有4 个对象。它将返回 4 行
标签: ios arrays swift uitableview tableview