Thiha Aung
Thiha Aung

Reputation: 5106

Array index out of range in cellForRowAtIndexPath

In my LeftViewController which was slide out menu,there was table view I am creating table header for each variable which will apply in table cell

var locals:[Local]=[Local(title: "Market",image:"ic_cars_black_24dp.png"),
                   Local(title: "Compare", image: "ic_bar_chart_24dp.png"),
                   Local(title: "Wishes",image: "ic_fantasy_24dp.png"),
                   Local(title: "Buy",image: "ic_put_in_24dp.png")]

var globals:[Global]=[Global(title: "Auction Latest",image:"ic_cars_black_24dp.png"),
                   Global(title: "Auction Past", image: "ic_bar_chart_24dp.png"),
                   Global(title: "Auction Recent",image: "ic_fantasy_24dp.png"),
                   Global(title: "Buy",image: "ic_put_in_24dp.png")]

These are my function that concern with UITableView

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.locals.count + self.globals.count
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerMenuCell = tableView.dequeueReusableCellWithIdentifier("HeaderMenuCell") as HeaderMenuCell

    switch(section){
    case 0:
        headerMenuCell.headerMenuLabel.text="Local"
    case 1:
        headerMenuCell.headerMenuLabel.text="Auction"
    default:
        headerMenuCell.headerMenuLabel.text="Others"
    }

    return headerMenuCell
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as Cell
    switch(indexPath.section){
    case 0:
        cell.configureForLocal(locals[indexPath.row])
    case 1:
        cell.configureForGlobal(globals[indexPath.row])
    default:
        cell.textLabel?.text="Others"
    }
    return cell
}

This is my Cell class

class Cell: UITableViewCell {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var imageNameLabel: UILabel!

func configureForLocal(locals: Local) {
    imageView.image = UIImage(named: locals.image)
    imageNameLabel.text = locals.title
}

func configureForGlobal(globals: Global) {
    imageView.image = UIImage(named: globals.image)
    imageNameLabel.text = globals.title
}

}

Please help,why i am occuring array index out of range?

Upvotes: 3

Views: 2309

Answers (1)

Alex Brown
Alex Brown

Reputation: 42902

tableView:numberOfRowsInSection: has a section argument which is the section for which it wants the row count—you need to return the correct count for the specified section, not the total count for all sections.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0: return locals.count
    case 1: return globals.count
    default: fatalError("unknown section")
    }
}

Otherwise it looks fine.

Upvotes: 3

Related Questions