1

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 !

1
  • I get no error when I run your code, but it doesn't populate the cells like I think you want. What you get from top to bottom is 5, 5, 3, 3, 3, "Section 2". 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). Commented Jun 3, 2015 at 23:43

2 Answers 2

4

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).

Sign up to request clarification or add additional context in comments.

6 Comments

@Abdou023 I don't know what you mean by "indexed from bottom to top" -- they are not. What exact error do you get if you use the first commented out line in the else clause?
I get the same error for whatever line I go with : "fatal error: Array index out of range"
@Abdou023 then your array must not be what you say it is. If you log titles, what does it give you?
It's the array I have in my first post, the same one you used in your answer. Section 1 works fine, if I commented out all the lines in section 3
@Abdou023 then I have no idea what's wrong. I tested the code I posted, and it works. Without seeing your entire actual code, I can't comment further.
|
1

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

3 Comments

It's intentional, the extra cell in the middle section should get its data from a different source.
Ah okay. So are you accounting for the missing row index when trying to retrieve data of your values array?
My guess is you're trying to pull a value out of your values array at index 5, but your values array doesn't have an index 5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.