Cosmo
Cosmo

Reputation: 930

UITableView with multiple sections shows only one section

I have a problem which seems to be very simple. I have a UITableView in my APP. In numberOfSectionsInTableView: I set the count of sections to 2. However the tableView shows only the first section. I tried to make the two sections show same contents by writing same codes in both sections, but useless. Then I found a similar question here. But its correct answer is about frame and my layout is a very simple UITableViewController. At last I tried to add NSLog to the three methods. The result is: For case section 1, numberOfSectionsInTableView: and numberOfRowsInSection: are called but cellForRowAtIndex isn't called. I wonder how to explain this.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    int count;
    switch (section) {
        case 0:
            count = [_objectArray count];
            break;
        case 1:
            NSLog(@"section called");       // This line is logged
            count = 1;
        default:
            count = 0;
            break;
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"myCellIdentifier";
    static NSString *basicCellIdentifier = @"myBasicCellIdentifier";

    switch ([indexPath section]) {
        case 0: {
            TableViewStoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                   forIndexPath:indexPath];

            // Configure the cell...
            int theIndex = [indexPath row];
            MyObject *theObj = [_objectArray objectAtIndex:theIndex];
            cell.object = theObj;
            return cell;
            break;
        }
        case 1: {
            NSLog(@"cell called");      //this line isn't logged
            for (int i = 0; i < 50; i++) {
                NSLog(@"hahaha");
            }
            MyBasicViewStoryCell *cell = [tableView dequeueReusableCellWithIdentifier:basicCellIdentifier forIndexPath:indexPath];
            return cell;
            break;
        }
        default: {
            for (int i = 0; i < 50; i++) {
                NSLog(@"hahaha1");
            }
            return nil;
            break;
        }
    }
}

Upvotes: 0

Views: 1687

Answers (3)

Zubair-Systematix
Zubair-Systematix

Reputation: 343

cellForRowAtIndexPath won't be called if numberOfRowsInSection is not returning a value that is >=1.

Upvotes: 0

Akhilrajtr
Akhilrajtr

Reputation: 5182

Put break in switch case. You missed break in case 1: so the default will also execute. Thus the count become zero count = 0. Since numberOfRowsInSection: returns zero, cellForRowAtIndexPath: will not call. So put break in case 1: of numberOfRowsInSection:

switch (section) {
    case 0:
        count = [_objectArray count];
        break;
    case 1:
        NSLog(@"section called");       // This line is logged
        count = 1;
        break;                          // Here
    default:
        count = 0;
        break;
}

Upvotes: 2

Gordon Childs
Gordon Childs

Reputation: 36074

You don't have a break after you set the count for section 1 and it falls through and is set to zero.

    case 1:
        NSLog(@"section called");       // This line is logged
        count = 1;
        break; // always put break!

Upvotes: 1

Related Questions