tiw
tiw

Reputation: 565

NSRangeException while changing uitableview data

I am both new here and in IOS programming, so please bear with my newbee questions for a while :]

Here is the situation of my application before my question:

I have a view in which i added 3 different subviews. (1-categories, 2-words and 3-rules) Within the categories subview I have a table of categories. (reads and writes from a categories plist and array)

According to the selected category I am changing the table ingredients for words table in the words subview. This table keeps the words the user enters for a selected category. (reads and writes from an nsmutabledictionary whose keys are the category names from the categories table and whose values are arrays of strings which were pre-entered by me to the plist)

Now an example to make things clear: my categories array has: Size, Color my words array for Size category has 3 strings in it like so: "tiny" , "big", "huge" my words array for Color category has 2 strings in it like so: "red", "blue"

First when i select "Size" category, i go and fetch words within that category and when i move from categories subview to words subview i see everything inside the "Size" category correctly in my uitableview. (it prints "tiny", "big" and "huge")

But when i go back to my categories subview and select "Color" this time and go back to words subview i get a crash.

Seems like it calls tableView:cellForRowAtIndexPath: method, and i get an NSRangeException as below:

[NSMutableArray objectAtIndex:]: index 2 beyond bounds

Long story short, I want to know why the cellForRowAtIndexPath method is calling an index which is not there. Here is how inside my cellForRowAtIndexPath method looks like:

[self reinitializeWordsDictionary];
[self reinitializeWordsInSelectedCategoryArray];

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [wordsInSelectedCategoryArray objectAtIndex:indexPath.row];

return cell;

Thanks in advance for the answers,

tiw

Upvotes: 0

Views: 1879

Answers (3)

WoodenKitty
WoodenKitty

Reputation: 6529

If anyone reading this has NSRangeExceptions not thrown by cellForRowAtIndexPath, check to ensure your UITableView isn't wrongly set to "static cells" content.

Upvotes: 3

dasdom
dasdom

Reputation: 14063

What are you giving back in

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

?

Upvotes: 0

GorillaPatch
GorillaPatch

Reputation: 5057

Sounds to me that the - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; method of the UITableView's data source is delivering wrong values. I would check that by setting a break point on this method and evaluate the returned values.

Upvotes: 0

Related Questions