Abdou023
Abdou023

Reputation: 1664

UITableView - different rows in sections with same array

I have an array which I get its values from a backend, and I have a UITableView with 3 sections and different rows :

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 3
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.

    if section == 1 {

        return 2
    } 
    else if section == 2 {

        return 1

    } else {

        return 3
    }

}




override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let myCell: CellTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CellTableViewCell

    // Configure the cell...

    if indexPath.section == 1 {

        myCell.titleLabel.text = titles[indexPath.row + 3]

    }
    else if indexPath.section == 2 {

        myCell.textLabel?.text = "Section 2"

    }
    else {


        myCell.titleLabel.text = titles[indexPath.row] // ERROR!

    }

    return myCell
}


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return "Section"
}

I need to have the cells from 2 different sections to get their data from the same array, and I have a section in the middle which should get it's data from a different array. When I run I get this error:

fatal error: Array index out of range

my array contains 5 values, it goes like this:

 titles = ["1", "2", "3", "4", "5"]

PS: I have custom cells for my TableView

Edit: I edited the sections arrangements ( first time I knew that they are indexed from bottom to top), but I still get and error with my last section cells, also there is a very weird behaviour where when I scroll down then back up, section 2 replaces the 3rd cell of section 1, and it gets moved to the bottom of the table !

Upvotes: 1

Views: 2527

Answers (2)

rdelmar
rdelmar

Reputation: 104082

You can't use loop like you're doing since that just gives each cell in that section the value in the last index that you loop through (index 2, or 4). Normally, for a sectioned table view, you would use an array of arrays to populate the sections. If you want to use a single array, and the number of rows in each section stay like you show them, then the following should work,

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var titles = ["1", "2", "3", "4", "5"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if section == 0 {

            return 3
        }
        else if section == 1 {

            return 1

        } else {

            return 2
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let myCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

        if indexPath.section == 0 {
            myCell.textLabel!.text = titles[indexPath.row]
        }
        else if indexPath.section == 1 {
            myCell.textLabel!.text = "Section 2"
        }
        else {
            myCell.textLabel!.text = titles[indexPath.row + 3]
        }

        return myCell
    }

}

This gives 1, 2, 3, "Section 2", 4, 5 for the six cells (I used a standard UITableViewCell for the test case).

Upvotes: 4

Zach Dennis
Zach Dennis

Reputation: 1784

Your array has 5 values, but your code suggests you have 6. Check out your tableView(tableView: UITableView, numberOfRowsInSection section: Int).

Upvotes: 1

Related Questions