I'm trying to have a Table view mixed with static and dynamic sections. My dynamic section use a Xib file/class.
The output should look like that Result of the working app
Section 1 and 3 are static and section 2 is dynamic and display contact's names (from the nib).
I based my code on the example of Shan Ye on the page UITableView Mix of Static and Dynamic Cells?
on the simulator, i have 3 contacts and on the iPhone, my array has 4 to display. I print a comment with the name in the cellForRowAt and it's printing all the 4 names.
I get the right result on the simulator and it crashes on the iPhone with the error
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
I'm using Swift 4, Xcode 9.3 on an iPhone X with IOS 11.3.
My code is
import UIKit
import Contacts
class EditEvenementTableViewController: UITableViewController {
var événement: Evenement? = nil
override func viewDidLoad() {
super.viewDidLoad()
addData() // fill an array for test
let nib = UINib.init(nibName: "DefaultParticipantTableViewCell", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier: "DefaultParticipantTableViewCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
} // numberOfSections
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 1 {
return (self.événement?.defaultParticipants.count)!
}
return super.tableView(tableView, numberOfRowsInSection: section)
} // numberOfRowsInSection
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 1 {
let participant = (événement?.defaultParticipants[indexPath.row])!
let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultParticipantTableViewCell") as! DefaultParticipantTableViewCell
cell.nomCompletLabel.text = participant.nomComplet
return cell
}
return super.tableView(tableView, cellForRowAt: indexPath)
} // cellForRowAt
} // EditEvenementTableViewController
Only suggestion or comment ?
Thanks