TableView 数组 returns 空
TableView Array returns null
我在 ViewController
中实现了 UITableview
。我想在通过 JSON
.
获取的 TableView
中显示一些数据
- (void)getData
{
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
for (NSDictionary*appDict in entryarr) {
NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
NSURL*imageURL=[NSURL URLWithString:imageStr];
NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
[self.imageArray addObject:imageData];
[self.tittleArray addObject:title];
[self.termArray addObject:title];
}
NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
}];
[dataTask resume];
}
现在,当我将数组分配给 TableView 单元格时,它显示数组为空。
但是在getData方法中Array由10个元素组成。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tittleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString*reUse=@"use";
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:reUse];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reUse];
cell.textLabel.textColor = [UIColor orangeColor];
}
cell.textLabel.text=[self.tittleArray objectAtIndex:indexPath.row];
cell.imageView.image=[self.imageArray objectAtIndex:indexPath.row];
return cell;
}
为什么会这样?我该怎么做。?谁能给我提供有效的解决方案?
重新加载您的 table 块内
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
for (NSDictionary*appDict in entryarr) {
NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
NSURL*imageURL=[NSURL URLWithString:imageStr];
NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
[self.imageArray addObject:imageData];
[self.tittleArray addObject:title];
[self.termArray addObject:title];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView Reloaddata];
});
NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
}]; [dataTask resume];
}
只需从 dataTaskWithURL
的完成处理程序重新加载您的 tableView
。
并等待 Web 服务调用完成,因为它是异步调用,所以您的 reloaddata
tableview 会在一段时间后被调用。如果数据量较大或网络连接速度较慢,可能需要很长时间。
您可以在完成处理程序中放置断点以检查它何时被调用。
您也可以显示 activity 指标。
其次确保您的标题、imageStr 或 imageData 不为 nil 或 null。因为您只是打印数组的计数。如果你在数组中添加空 object 那么它会增加计数但它的实际值将为空白。
所以,凡事都要妥善管理,当你做错的时候,你就会发现。
如果数组计数大于 0,则调用重新加载数据
参考下面的代码
-(void)getData
{
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
for (NSDictionary*appDict in entryarr) {
NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
NSURL*imageURL=[NSURL URLWithString:imageStr];
NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
[self.imageArray addObject:imageData];
[self.tittleArray addObject:title];
[self.termArray addObject:title];
}
NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
}];
[dataTask resume];
// add reload data
if([self.termArray count] >0)
// call reload data here
}
在调用API.
之前简单地写你的数组对象的like, alloc 和init object
喜欢,
NSMutableArray *titleArray = [[NSMutableArray alloc] init];
将您正在使用的所有数组放入 CellForRowAtIndexPath 方法中以显示记录。
并在从 Web 服务获取所有记录后重新加载您的 table 查看数据。
注意:根据您的问题,您得到的是空数组,因此请检查给定的解决方案。
嗯,虽然所有其他答案都建议添加
[self.tableView reloadData];
如果您的块在主线程中 运行 可能已经解决了问题,但是由于您正在执行转到另一个线程的网络操作,您将无法修改 UI从那个线程。您可能想这样做:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
首先,这是一个基本问题,您甚至没有调用重新加载函数。另一个问题是对事物如何运作的基本理解。因此,每当您的模型准备就绪并已填充但您的 UI 没有更改时(假设您调用函数来更改 UI),请尝试查看日志输出,有时它说您无法更改UI 在另一个线程中,并检查您编码的线程是 运行。
我在 ViewController
中实现了 UITableview
。我想在通过 JSON
.
TableView
中显示一些数据
- (void)getData
{
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
for (NSDictionary*appDict in entryarr) {
NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
NSURL*imageURL=[NSURL URLWithString:imageStr];
NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
[self.imageArray addObject:imageData];
[self.tittleArray addObject:title];
[self.termArray addObject:title];
}
NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
}];
[dataTask resume];
}
现在,当我将数组分配给 TableView 单元格时,它显示数组为空。 但是在getData方法中Array由10个元素组成。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tittleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString*reUse=@"use";
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:reUse];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reUse];
cell.textLabel.textColor = [UIColor orangeColor];
}
cell.textLabel.text=[self.tittleArray objectAtIndex:indexPath.row];
cell.imageView.image=[self.imageArray objectAtIndex:indexPath.row];
return cell;
}
为什么会这样?我该怎么做。?谁能给我提供有效的解决方案?
重新加载您的 table 块内
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
for (NSDictionary*appDict in entryarr) {
NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
NSURL*imageURL=[NSURL URLWithString:imageStr];
NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
[self.imageArray addObject:imageData];
[self.tittleArray addObject:title];
[self.termArray addObject:title];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView Reloaddata];
});
NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
}]; [dataTask resume];
}
只需从 dataTaskWithURL
的完成处理程序重新加载您的 tableView
。
并等待 Web 服务调用完成,因为它是异步调用,所以您的 reloaddata
tableview 会在一段时间后被调用。如果数据量较大或网络连接速度较慢,可能需要很长时间。
您可以在完成处理程序中放置断点以检查它何时被调用。
您也可以显示 activity 指标。
其次确保您的标题、imageStr 或 imageData 不为 nil 或 null。因为您只是打印数组的计数。如果你在数组中添加空 object 那么它会增加计数但它的实际值将为空白。
所以,凡事都要妥善管理,当你做错的时候,你就会发现。
如果数组计数大于 0,则调用重新加载数据 参考下面的代码
-(void)getData {
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
for (NSDictionary*appDict in entryarr) {
NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
NSURL*imageURL=[NSURL URLWithString:imageStr];
NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
[self.imageArray addObject:imageData];
[self.tittleArray addObject:title];
[self.termArray addObject:title];
}
NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
}];
[dataTask resume];
// add reload data
if([self.termArray count] >0)
// call reload data here
}
在调用API.
之前简单地写你的数组对象的like, alloc 和init object喜欢,
NSMutableArray *titleArray = [[NSMutableArray alloc] init];
将您正在使用的所有数组放入 CellForRowAtIndexPath 方法中以显示记录。
并在从 Web 服务获取所有记录后重新加载您的 table 查看数据。
注意:根据您的问题,您得到的是空数组,因此请检查给定的解决方案。
嗯,虽然所有其他答案都建议添加
[self.tableView reloadData];
如果您的块在主线程中 运行 可能已经解决了问题,但是由于您正在执行转到另一个线程的网络操作,您将无法修改 UI从那个线程。您可能想这样做:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
首先,这是一个基本问题,您甚至没有调用重新加载函数。另一个问题是对事物如何运作的基本理解。因此,每当您的模型准备就绪并已填充但您的 UI 没有更改时(假设您调用函数来更改 UI),请尝试查看日志输出,有时它说您无法更改UI 在另一个线程中,并检查您编码的线程是 运行。