I have a UITableView . I try to display the cells of the first and only section by setting their values ​​dynamically. Here is my controller:

 import UIKit class SettingsController: UITableViewController { var sections = [10, 15, 30] override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return sections.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("minutesIntervalCell", forIndexPath: indexPath) cell.textLabel?.text = String(sections[indexPath.row]) return cell } } 

Here is what I have in Main.stroyboard:

enter image description here

When opening this view, this error:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** - [__ NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

I understand the problem here is that there is only one cell in the view itself, and I try to set three, but I’m in the code saying that there will be three cells, and he has the feeling that he still believes that there is only one cell.

I already ditch my hair what to do. Please, help.

    2 answers 2

    It seems that in your UITableViewController settings in Main.storyboard, the cell type is set to static, not dynamic. Check out this moment.

    In addition, there are two methods for creating cells: tableView.dequeueReusableCellWithIdentifier("minutesIntervalCell", forIndexPath: indexPath) will try to create a cell without checking for free caches. This method requires the cell and identifier to be registered by a preliminary call to the tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "minutesIntervalCell") method tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "minutesIntervalCell") .

    The second method tableView.dequeueReusableCellWithIdentifier("minutesIntervalCell") highly recommended because, first, it checks for the presence of cells in the cache, which has a positive effect on performance, and, second, it is not limited by additional requirements and returns Optional .

    • It turned out that the static type was indeed specified. Ferried to the dynamic and everything became normal. Thank you very much! What little things sometimes become hellish trials when you do something for the first time. - iserdmi

    You must indicate in the Storyboard that the DataSource and Delegate your tableView = your ViewController

    For UITableViewController they are by default, but you need to check whether you have disabled them.