Mohamed Emad Hegab
Mohamed Emad Hegab

Reputation: 2675

iphone : proplem with table view

I have a problem with table view in iphone .. i can't figure out why it crashes everytime will here is the code

   - (void)viewDidLoad
{
     [self checkAndCreatePList];
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:pListPath];

    self.animals = [plistDict objectForKey:@"Animals"];



         [super viewDidLoad];

}



    -(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
    static NSString *SimpleTableIdentifier =@"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if(cell== nil){
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier]autorelease];
    }
    NSUInteger row = [indexPath row];

    cell.textLabel.text = [animals objectAtIndex:row];

    return cell;
}

it's crashes at the line cell.textLabel.text = [animals objectAtIndex:row]; and tells me that Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance

Upvotes: 0

Views: 91

Answers (4)

user745098
user745098

Reputation:

Looks like animals is some dictionary and you are calling objectAtIndex: method on it. objectAtIndex: is NSArray method.

Upvotes: 0

saadnib
saadnib

Reputation: 11145

the error seems that you are calling objectAtIndex on a NSDictionary object at line cell.textLabel.text = [animals objectAtIndex:row]; check what does animal contains at run time. For this use NSLog before this line. NSLog(@"%@",animals);

Upvotes: 0

MarkPowell
MarkPowell

Reputation: 16530

[plistDict objectForKey:@"Animals"];

is returning a Dictionary not an Array like you are expecting. You need to check out your plist file to see if the data is correct.

Upvotes: 0

Jim
Jim

Reputation: 73936

The Animals key in your plist refers to a dictionary, not an array. Dictionaries don't have a guaranteed order, so asking for an object at a particular index doesn't make sense.

In addition to this, you have a memory leak - plistDict is allocated but never released. Have you run the static analyser over your code?

Upvotes: 2

Related Questions