Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableview in UITableviewcontroller - cellforrowatindexpath not called on reload

I am stucked in a stupid problem since two days. I have got a UITableViewController pushed in Navigation Controller. When it loads, since there is no data, so empty table is visible:

But when I receive data from server, and call [self.tableView reloadData], both numberOfRowsInSection and heightForRowAtIndexPath get invoke except cellForRowAtIndexPath and my controller is shown without table:

I can't really understand that why it is happening. All datasource methods are called except for cellForRowAtIndexPath. Please someone guide me... Thanks..

ActLogController.h

@interface ActLogController : UITableViewController<ASIHTTPRequestDelegate,UITableViewDataSource,UITableViewDelegate>

@property(strong) NSMutableArray *activityKeys;

@end

ActLogController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    activityKeys = [[NSMutableArray alloc] init];
    self.tableView.dataSource = self;
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self retrieveActivityLogFromServer];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

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

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    ActivityLogUnit *act = [activityKeys objectAtIndex:indexPath.row];
    cell.textLabel.text = act.price;

    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0;
}

-(void) requestFinished:(ASIHTTPRequest *)request
{
    NSArray *list = [request.responseString JSONValue];

    for (int i = 0; i < list.count; i++) {

        NSArray *singleTrade = [[list objectAtIndex:i] JSONValue];

        ActivityLogUnit *unit = [[ActivityLogUnit alloc] init];

        unit.symbol = [singleTrade objectAtIndex:0];
        unit.type = [singleTrade objectAtIndex:1];
        unit.price = [singleTrade objectAtIndex:2];
        unit.volTraded = [singleTrade objectAtIndex:3];
        unit.remVol = [singleTrade objectAtIndex:4];
        unit.actualVol = [singleTrade objectAtIndex:5];
        unit.recordID = [singleTrade objectAtIndex:6];
        unit.orderNo = [singleTrade objectAtIndex:7];
        unit.time = [singleTrade objectAtIndex:8];

        [activityKeys addObject:unit];

    }

    if(activityKeys.count > 0)
    {
        [self.tableView reloadData];//it is called and I have got 6 items confirm
    }
}

EDIT

I set some dummy data in my array activityKeys, Data is being displayed in table, and cellforrowatindexpath is called successfully. But as I reload data after sometime, other methods are called except this one and table disappears as shown in 2nd pic. Any ideas?

like image 505
NightFury Avatar asked Dec 05 '22 10:12

NightFury


1 Answers

Your problem is that you probably download the data content on a background thread. Since you cannot update the UI on a background you need to call [self.tableView reloadData] on the main thread once the download is finished!

Hope it helps!

like image 174
Groot Avatar answered Dec 30 '22 14:12

Groot