user3205189
user3205189

Reputation:

Load array into a tableView?

I do this all the time but for some reason its not working for me today.

The cells are empty , here is my code:

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

    return [self.messageArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell..
    NSString *messageAtIndexPath = [messageArray objectAtIndex :[indexPath row]];
    [[cell textLabel] setText:messageAtIndexPath];

    return cell;
}

I made sure everything (Delegate and Source) is connected properly.

If I hardcode stuff for example:

return 5;

and

[[cell textLabel] setText:@"this is a cell"];

Then its working , so I am not sure whats up. Also my array is populated I can see it in the log console.

EDIT:

dispatch_async(queue, ^{
    NSString *postString = @"controller=Push&action=GetPushAlerts&accountId=123";
    NSData *JsonData = [ApiCaller post :postString];
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:JsonData options:NSJSONReadingMutableContainers error:nil];
    //parsing JSON

    dispatch_async(dispatch_get_main_queue(), ^{

        BOOL success = [result[@"success"] boolValue];
        if(success){
            // Account exists, parse the data
            NSDictionary *data = [result objectForKey:@"data"];
            messageArray = [data objectForKey:@"message"];

           // NSLog(@"count %@", message);

        }

    });
});

Upvotes: 0

Views: 162

Answers (2)

Lord Zsolt
Lord Zsolt

Reputation: 6557

So to wrap things up: If you're loading data asynchronously, you MUST reload the table View (Or anything that displays the data and is drawn before the data arrives), because when a view loads, it displays according to the data it has at THAT instant.

To do this, add the following command to where the data arrives (usually into the success block, unless you want to display some data on the failure block as well):

[self.tableView reloadData]; 

If on the other hand, the call is synchronous (rarely used), it hangs the application where the call is executed (usually in viewDidLoad), so the display is only drawn after the data arrived, thus having data to display.

Upvotes: 1

Jorden
Jorden

Reputation: 13

It could be that the cells are defined as static in IB rather than dynamic? You should check that first.

Upvotes: 0

Related Questions