【问题标题】:Reloading tableview after fetching asynchronous JSON data获取异步 JSON 数据后重新加载 tableview
【发布时间】:2012-04-23 02:23:45
【问题描述】:

我使用异步 JSON 请求和响应来获取一些数据。我想在表格视图中填充它。但是,我看到的只是空白表格单元格。我试着做一个reloadData,但它不起作用。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://strong-earth-32.heroku.com/stores.aspx"]];//asynchronous call
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    SBJsonParser *parser = [[SBJsonParser alloc]init];
    resultData = [[parser objectWithString:responseString error:nil]copy];


    NSArray *storeArray = [[NSArray alloc]init];
    storeArray= [resultData objectForKey:@"stores"];
    populatedStoreArray = [[NSMutableArray alloc]init];
    images = [[NSMutableArray alloc] init];

...........

[populatedStoreArray addObject:tempStore];
    } 
    [self.storeDetailsTable reloadData];
        [parser release];

}

tableview 如下所示:

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

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StoreCellView" owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (StoreCell *) currentObject;
                break;
            }
        }

    }

    Store *storeAtIndex = [populatedStoreArray objectAtIndex:[indexPath row]];
    cell.storePhoneNumber.text = [storeAtIndex phone];
    cell.storeAddress.text = [storeAtIndex address];
    cell.storeImage.image = [images objectAtIndex:[indexPath row]];
    return cell;
}

我的更多代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [populatedStoreArray count];

}

头文件:

@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    IBOutlet UITableView *storeDetailsTable;

}

@property (nonatomic, retain) IBOutlet UITableView *storeDetailsTable;
@property (nonatomic, strong) NSDictionary *resultData;
@property (nonatomic, strong) NSMutableArray *populatedStoreArray;
@property (nonatomic, strong) NSMutableArray *images;
@property (nonatomic, strong) NSMutableData *responseData;


@end

任何指针都将不胜感激,因为在我搜索的任何地方,我都被告知使用reloadData,但它不起作用。其实xcode在我使用的时候连提示都没有。

谢谢。

【问题讨论】:

  • tableView的dataSource的方法你实现了吗?你能展示更多关于你的实现的代码吗?
  • 我发现出了什么问题,结果发现 reloadData 只重新加载已经存在的单元格。我想用比以前更多的单元格重新加载表格。这怎么可能?
  • 当你回答 numberOfRowsInSection: 时,它会重新加载你告诉它存在的所有行。该方法应该回答您填充的StoreArray 的计数。
  • 确实如此。问题是,当我重新加载表视图时,我已经将更多数据添加到了populatedStoreArray 中。因此,需要更新计数。我该如何做到这一点?
  • storeDetailsTable tableview 是否正确链接到您的 iboutlet?

标签: iphone ios xcode uitableview reloaddata


【解决方案1】:

也许你错过了这一行:

storeDetailsTable.delegate=self;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 2014-05-09
    • 2011-10-18
    • 1970-01-01
    • 2020-07-31
    相关资源
    最近更新 更多