UITableviewcontroller中的UITableview - 在重新加载时不调用cellforrowatindexpath

Nig*_*ury 4 reload uitableview ios

两天以来,我陷入了一个愚蠢的问题.我有一个UITableViewController推进Navigation Controller.加载时,由于没有数据,因此可以看到空表:

但是,当我收到来自服务器的数据,并调用[self.tableView reloadData],都numberOfRowsInSectionheightForRowAtIndexPath得到调用,除了cellForRowAtIndexPath和我的控制器显示为无表:

我真的不明白为什么会这样.除了之外,调用所有数据源方法cellForRowAtIndexPath.请有人指导我......谢谢..

ActLogController.h

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

@property(strong) NSMutableArray *activityKeys;

@end
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

我在我的数组中设置了一些虚拟数据activityKeys,数据显示在表中,并被cellforrowatindexpath 成功调用.但是当我在一段时间后重新加载数据时,会调用其他方法,除了这一个,表格消失,如第二张图所示.有任何想法吗?

Gro*_*oot 5

您的问题是您可能在后台线程上下载数据内容.由于无法在后台更新UI,因此下载完成后需要在主线程上调用[self.tableView reloadData]!

希望能帮助到你!


sha*_*eed 5

看起来你在辅助线程中,使用以下代码在主线程中执行reloadData

[self.tableView performSelectorOnMainThread@selector(reloadData) withObject:nil waitUntilDone:NO]
Run Code Online (Sandbox Code Playgroud)

您始终可以使用[NSThread isMainThread]来检查您是否在主线程中.