Gold Challenge Question


#1

I’m trying to meet the row height requirement of this challenge. The code posted below causes a crash. It seems as though sending the numberOfRowsInSection method to the tableView causes a recursive loop.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 60;

    NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
    NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;  // BLOWS UP!
    
    if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
        height = 44;
    }
    
    return height;
}

But if I change it just a little, it works.

NSInteger lastRowIndex = [self tableView:tableView numberOfRowsInSection:lastSectionIndex] - 1;  // WORKS!

Can someone explain what is happening here?


#2

I had same problem and hopefully someone can explain. According to API documentation, it should work.


#3

StackOverflow has an answer.