0

I am trying to make a tableview with cells split into sections with uitableview grouped. I have looked around and found out how to split it into sections though I am stuck on making them appear in groups like they should instead of three groups with all of the cells in each which is what I have at the moment this is what I have got so far after doing a bit of research:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    if ([indexPath section] == 0) {
        if (indexPath.row == 0) {
            cell = cell1;
        } else if (indexPath.row == 1) {
            cell = cell2;
        } else if (indexPath.row == 2) {
            cell = cell3;
        }   
    }
    if ([indexPath section] == 1) {
        if (indexPath.row == 0) {
            cell = cell4;
        } else if (indexPath.row == 1) {
            cell = cell5;
        } else if (indexPath.row == 2) {
            cell = cell6;
        }
    }
    if ([indexPath section] == 2) {
        if (indexPath.row == 0) {
            cell = cell7;
        }
    }
    return cell;
}

Though when I run and go to this view the application crashes and in the nslog box it comes up with this error:

*** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:5471

Thanks in advance

Hive7
  • 3,599
  • 2
  • 22
  • 38
  • This looks like a completely wrong approach. What are cell1, cell2, etc? You get sections by having your data source array be an array of arrays (usually). In number of sections, you return the count of your array, and in numberOfRows, you return the count of each inner array. – rdelmar Aug 17 '13 at 15:31
  • @rdelmar I am using uitableview cells that are on the xib and I am pulling them in. That's what they are – Hive7 Aug 17 '13 at 15:44
  • And do you have 7 different kinds of cells? – rdelmar Aug 17 '13 at 16:49
  • Yes I do and if I remove all things related to sections then it will work but of course without sections – Hive7 Aug 17 '13 at 16:50
  • You appear not to be adding any content in cellFroRowAtIndexPath. Are you adding the content in the xib? – rdelmar Aug 17 '13 at 16:52
  • Sorry for the extended answers though SO won't let me post otherwise – Hive7 Aug 17 '13 at 17:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35665/discussion-between-rdelmar-and-ollie-cole) – rdelmar Aug 17 '13 at 17:54

1 Answers1

1

The row indexes start at zero for each new section.

For example:

if ([indexPath section] == 2) {
    if (indexPath.row == 3) {

Should be:

if ([indexPath section] == 2) {
    if (indexPath.row == 0) {

Furthermore, section indexes also start at zero so you probably want to decrement each index in your if statements.

cutsoy
  • 10,127
  • 4
  • 40
  • 57