Khledon
Khledon

Reputation: 181

table view numberOfRowsInSection returning 0

I have a table view which I wish to populate with data stored in parse.com.

However nothing loads in my table view. I've done a few breakpoints and NSLog's and found that my numberOfRowsInSection method is always returning 0.

Here is my code:

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

        int num = [_totalDistance count];
        NSLog(@"num = %d", num);
        return [_totalDistance count];

}

activityTypeArray is populated from data collected from parse.com in my viewDidLoad method, is it possible this is the problem?

Here is where _totalDistance is populated:

- (void)viewDidLoad
{
    _activityTypeArray = [NSMutableArray array];
    _totalDistance = [NSMutableArray array];
    PFQuery *activityQuery = [PFQuery queryWithClassName:@"Activities"];

    [activityQuery whereKey:@"triathlete" equalTo:[PFUser currentUser]];

    [activityQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (PFObject *object in objects) {

                ;
                NSString *activityType = [object objectForKey:@"activityType"];
                [_activityTypeArray addObject:activityType];

                NSString *totalDistance = [object objectForKey:@"totalDistance"];
                [_totalDistance addObject:totalDistance];
                NSLog(@" total distance %@", _totalDistance);
                int num = [_activityTypeArray count];
                NSLog(@"num = %d", num);
            }

        }
    }];


}

Thanks

Upvotes: 1

Views: 1360

Answers (2)

Nikita Took
Nikita Took

Reputation: 4012

Modify your code to reload tableView data. Otherwise your tableView won't know you have data to show.

   [activityQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (PFObject *object in objects) {

                ;
                NSString *activityType = [object objectForKey:@"activityType"];
                [_activityTypeArray addObject:activityType];

                NSString *totalDistance = [object objectForKey:@"totalDistance"];
                [_totalDistance addObject:totalDistance];
                NSLog(@" total distance %@", _totalDistance);
                int num = [_activityTypeArray count];
                NSLog(@"num = %d", num);

                // line added
                [self.tableView reloadData];
            }

        }
    }];

Upvotes: 2

Stefan
Stefan

Reputation: 5461

You are logging another value (_activityTypeArray count) than you are returning as result (_totalDistance count)

So the NSLog statement cannot help

Upvotes: 1

Related Questions