ASH
ASH

Reputation: 253

uitableview throws bad access error object at indexpath

I keep getting this error:

2013-03-30 19:48:40.029 try[14838:907] -[__NSSetM objectAtIndex:]: unrecognized selector sent to instance 0x1e8ea660 2013-03-30 19:48:40.030 try[14838:907] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSetM objectAtIndex:]: unrecognized selector sent to instance 0x1e8ea660' * First throw call stack: (0x33e523e7 0x3bb43963 0x33e55f31 0x33e5464d 0x33dac208 0xe5bc7 0x35cac569 0x35c91391 0x35ca8827 0x35c648c7 0x35a10513 0x35a100b5 0x35a10fd9 0x35a109c3 0x35a107d5 0x35a10639 0x33e27941 0x33e25c39 0x33e25f93 0x33d9923d 0x33d990c9 0x3797733b 0x35cb52b9 0x7b495 0x3bf70b20) libc++abi.dylib: terminate called throwing an exception (lldb)

// Heres my code:

- (void)fetchTweets
{

    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: @"***.json"]];

        NSError* error;



        tweets = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tweets.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"showVideo";

    CustomVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(!cell)
    {
        cell = [[CustomVideoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
    av.backgroundColor = [UIColor clearColor];
    av.opaque = NO;
    av.image = [UIImage imageNamed:@"cell_bg.png"];
    cell.backgroundView = av;


    UIImage *image = [UIImage   imageNamed:@"disclosure_arrow_white.png"];
    UIImageView *av2 = [[UIImageView alloc] initWithFrame:CGRectMake(44.0, 44.0, image.size.width, image.size.height)];
    av2.backgroundColor = [UIColor clearColor];
    av2.opaque = NO;
    av2.image = [UIImage imageNamed:@"disclosure_arrow_white.png"];
    cell.accessoryView = av2;



    NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];


    cell.title.text = [tweet objectForKey:@"title"];


    NSLog(@"t: %@",[tweet objectForKey:@"title"]);


    return cell;

}

Upvotes: 0

Views: 686

Answers (2)

Anupam
Anupam

Reputation: 56

You can try retaining tweets viz.

tweets = [[NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error] retain];

and later release it in dealloc. I guess its an auto released object getting assigned to it.

Upvotes: 1

Matt
Matt

Reputation: 2411

After the line:

CustomVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

You need to check that cell actually has a value, and create a new cell if it doesn't.

CustomVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
  cell = [[CustomVideoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier];
}

Good luck!

UPDATE for your new error:

The 'tweets' variable resulting from JSONObjectWithData is an NSDictionary, not an NSArray. Since NSDictionary does not implement 'objectAtIndex', the unrecognized selector exception is thrown. Typically, the tweets dictionary should have a certain key that contains the array of tweets, which you should assign to the 'tweets' variable in your fetchTweets method.

If you're not sure about a lot of the stuff I am referring to, then I strongly recommend you use the Sensible TableView framework. The framework will be able to automatically fetch all the web service tweets for you and automatically display them on the table view. I personally always use it now, saves me a ton of manual labor.

Upvotes: 0

Related Questions