Reputation: 97
// Variable and ViewDidLoad
var auxRow = 0
var auxSection = 0
override func viewDidLoad() {
super.viewDidLoad()
}
//Number Of Sections, only one section
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
auxSection += 1
print("times section: ", auxSection)
return 1
}
//Number Of Rows
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
auxRow += 1
print("times row: ", auxRow)
return 2
}
// TableView Configure the cell
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
}
Output:
times section: 1
times row: 1
times section: 2
times row: 2
times section: 3
times row: 3
times section: 4
times row: 4
The method numberOfRowsInSection
is being called 4 times. Why is that happening?
Upvotes: 2
Views: 4033
Reputation: 1854
You can set a breakpoint inside your numberOfRowsInSection
method to see what it is being used for internally. There is no reason why it shouldn't be called multiple times, as it is called any time the tableview internally needs to know how many rows it has. It is called 4 times because Apple's tableview code needs to know, at 4 different times, how many rows are in your one section.
Upvotes: 2
Reputation: 3440
Apple makes no guarantees how often UIKit may call one of your delegate methods. They own the framework. We can expect them to cache that value but nothing requires them to do that.
Upvotes: 6