0
我已经使用异步JSON请求和响应来获取一些数据。我想在tableview中填充它。但是,我看到的只是空白表格单元格。我试图做一个reloadData
,但它不工作。这里是我的代码:获取异步JSON数据后重新载入tableview
- (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];
}
的实现代码如下如下:
- (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'的方法呢?你能否展示更多有关你的实现的代码? – AechoLiu 2012-04-23 02:26:55
我发现出了什么问题,结果reloadData只重新加载已经存在的单元格。我想用比以前更多的单元格重新加载表格。这将如何成为可能? – Shwethascar 2012-04-23 02:42:10
当您回答numberOfRowsInSection:时,它将重新加载您告诉它的所有行。该方法应该回答你的populatedStoreArray的数量。 – danh 2012-04-23 02:43:53