f4r4
f4r4

Reputation: 573

When scrolling in my UITableView cells are disappearing

This is my sample twitter program it works but there are several errors if I scroll up or down top and bottom cellls disappears some times I get an error saying :

BAD ACCESS CODE this happens on this row

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

Please Advice

enter image description here

#import "TableViewViewController.h"

@interface TableViewViewController ()

@end

@implementation TableViewViewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"tweets array count : %d", tweets.count);
    return tweets.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TweetCell";

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

    NSLog(@"ROW : %d", indexPath.row);

    NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
    NSString *text = [tweet objectForKey:@"text"];
    NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];

    cell.textLabel.text = text;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name];
    return cell;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self fetchTweets];
}

- (void)fetchTweets
{

    NSString *twitterURL = [NSString stringWithFormat:@"https://api.twitter.com/1/statuses/public_timeline.json"];    
    NSURL *fullURL = [NSURL URLWithString:twitterURL];

    NSError *error = nil;
    NSData *dataURL = [NSData dataWithContentsOfURL:fullURL options:0 error:&error];

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

.....

@end

Upvotes: 0

Views: 971

Answers (4)

f4r4
f4r4

Reputation: 573

Found out the answer My app doesn't support ARC because I didn't check if we use a retain at the NSJSONSerialization the error is fixed.

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

Upvotes: 1

Dustin
Dustin

Reputation: 6803

Why don't you just uncomment return tweets.count; in your numberOfRowsInSection method?

Upvotes: 0

Dima
Dima

Reputation: 23634

Why is numberOfRowsInSection hardcoded to 11?

Also you should have [self.tableView reloadData] after the tweets array is set up in fetchTweets

Upvotes: 0

Peter
Peter

Reputation: 1359

I see problems in this function:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"tweets array count : %d", tweets.count);
    //return tweets.count;
    return 11; //default returns 20 but only shows 10 indexpath.Row so 11
}

You probably shouldn't be just returning 11 -- what if tweets has a different length? The table view will try to fetch a cell at an index which doesn't exist in your array. Try returning tweets.count instead.

To elaborate a bit further: The purpose of the tableView:numberOfRowsInSection: method isn't to tell iOS how many rows there are on the screen, it's how many rows there are in the entire tableView. This includes cells which aren't shown on screen.

Upvotes: 3

Related Questions