【发布时间】:2014-11-12 11:58:51
【问题描述】:
下午好,
我正在使用 TableViewController 来显示我的数据库中的一些“条目”,并且我正在尝试使用 [self.tableView reloadData]; 以便在加载内容时重新加载表格,但事实并非如此工作,因为只有当我向上或向下触摸屏幕时才会显示内容,如果我不触摸就离开它不会显示任何内容。
我认为我已将 [self.tableView reloadData]; 放在正确的位置,因为由于某种原因直到我用手指移动屏幕后才会显示。
为什么会发生这种情况,我该怎么做才能自动重新加载我的表格视图?
CarTableViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchJson];
}
这就是我的 fetchJson:
-(void)fetchJson {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString * urlString = [NSString stringWithFormat:@"http://website.com/service.php"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
self.carModels = [[NSMutableArray alloc] init];
self.carMakes = [[NSMutableArray alloc] init];
self.carImages = [[NSMutableArray alloc] init];
self.likes = [[NSMutableArray alloc] init];
self.comments = [[NSMutableArray alloc] init];
@try
{
NSError *error;
[_jsonArray removeAllObjects];
_jsonArray = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for(int i=0;i<_jsonArray.count;i++)
{
NSDictionary * jsonObject = [_jsonArray objectAtIndex:i];
NSString* imagen = [jsonObject objectForKey:@"imagen"];
[_carImages addObject:imagen];
NSDictionary * jsonObject2 = [_jsonArray objectAtIndex:i];
NSString* user = [jsonObject2 objectForKey:@"user"];
[_carMakes addObject:user];
NSDictionary * jsonObject3 = [_jsonArray objectAtIndex:i];
NSString* date = [jsonObject3 objectForKey:@"date"];
[_carModels addObject:date];
}
}
@catch (NSException * e)
{
NSLog(@"Exception: %@", e);
}
@finally
{
[self.tableView reloadData];
}
}
);
}
提前致谢。
【问题讨论】:
-
显示你的:numberOfSectionsInTableView、numberOfRowsInSection、cellForRowAtIndexPath 方法。
-
您是否尝试过确保
self.tableView reloadData正在主线程上运行 -
用更多代码编辑@OlegSobolev。
-
如何查看 self.tableView.reloadData 是否正常工作?谢谢@Flexicoder
-
改成这个
dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData] });
标签: ios objective-c xcode uitableview xcode6