Gold Challenge Problem, Need Help


#1

I have gotten up to the changing size portion. I know that I could enumerate through the array with a conditional for price range, but i thought it would be easier to use [tableView numberOfRowsInSection:[indexPath section]] to find the last row. I know I have that property set correctly because it is showing up correctly when running. My code is as follows:

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int returnValue = 44;
    int numRows = [tableView numberOfRowsInSection:[indexPath section]];
    if (!([indexPath row] ==  numRows - 1)) {
        returnValue = 60;
    }
    return returnValue;
}

I have commented out all the lines and have discovered that the culprit of my issue is [tableView numberOfRowsInSection:[indexPath section]]. It doesn’t crash, but it does pause my build and tell me that EXC_BAD_ACCESS (code=2, address=0xbf7fff7c) Any ideas?


#2

well, the first thing is the return value should be a float and you are returning an int… :wink:
but i don’t think that’s the primary issue.

If you still have overridden -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Then that method is getting called. In my code, it gets called 1000’s of times but is not returning anything for the -rowHeight method to use.
When I inserted a static number then it worked OK: NSInteger numRows = 6;
I’m a little confused too.


#3

Yeah i have changed that in my implementation. It looks like I have made an infinite loop. Found this in the documentation:
"Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more)."
When I put in an NSLog to find out what row/section it was stuck on it just kept spitting out section 1 row 0 until it crashed. Maybe I can’t call a tableView method in this implementation because it calls itself in the process.


#4

I’ve run into a very similar problem. Has anyone found a solution to this?


#5

ok figured out that this method implementation works without the issues mentioned above…

the main difference is that in place of: ([tableView numberOfRowsInSection:[indexPath section]) in the condition for the if-statement, we have: ([[[BNRItemStore sharedStore] (items Array)] count])

so it looks like calling numberOfRowsInSection: somehow causes the infinite loop…

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 44;
    
    if (([indexPath section] == 0) && ([indexPath row] < [[[BNRItemStore sharedStore] itemsOverFifty] count])) {
        height = 60;
    }
    
    if (([indexPath section] == 1) && ([indexPath row] < [[[BNRItemStore sharedStore] itemsUnderFifty] count])) {
        height = 60;
    }
    
    return height;
}

#6

Yes, I faced same problem, the linker paused and there were several threading error (as if an endless loop was detected).

I read the next chapter (10) and numberOfRowsInSection:0 is used in UIButton without any error.
In chap 10, under ‘Adding row’ section:

[code]- (IBAction) addNewItem:( id) sender
{
// Make a new index path for the 0th section, last row
int lastRow = [[ self tableView] numberOfRowsInSection: 0];
NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection: 0];

// Insert this new row into the table.
[[ self tableView] insertRowsAtIndexPaths:[ NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationTop];
} [/code]

Looks like numberOfRowsInSection:0 function must be used carefully.

To get around this, I used the longer route, viz predicate and filter and count method.