ios - TableViewController 内容不显示内容(reloadData)

标签 ios objective-c xcode uitableview xcode6

下午好

我正在使用 TableViewController 来显示我数据库中的一些“条目”,并且我正在尝试使用 [self.tableView reloadData]; 来重新加载加载内容时显示表格,但它不起作用,因为只有当我向上或向下触摸屏幕时才会显示内容,如果我不触摸屏幕就不会显示任何内容。

我想我已经把 [self.tableView reloadData]; 放在了正确的位置,因为出于某种原因直到我用手指移动屏幕才显示。

为什么会这样,我该怎么做才能自动重新加载我的 tableview?

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];
        }
    }
    );
}

提前致谢。

最佳答案

当我开始开发 iOS 应用程序时,我也经常遇到这个问题。

问题是,您没有在负责 UI 的主线程上运行 reloadData。事实上,如果你运行这个方法(不在主线程中),你的 tableView 的数据实际上会重新加载,但不会显示给用户。因此,如果您开始滚动 tableView,主线程将“绘制”您的用户界面,从而显示数据。 所以你必须在主线程上运行reloadData

所以取出

[self.tableView reloadData];

并在成功从数据库中获取数据(并且值存储在字典/数组中)后添加这些代码行:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
});

我认为您甚至可以将该代码放入您的@finally block 中。 main main_queue 包含在主线程上运行的任务。

关于ios - TableViewController 内容不显示内容(reloadData),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26886452/

相关文章:

objective-c - IOS8 - [UIlabel sizeToFit] 导致 EXC_BAD_ACCESS KERN_INVALID_ADDRESS 崩溃

ios - UILabel 从具有动态类型的静态 UITableViewCell 中消失

ios - 使用核心动画后如何获取 View 的当前旋转?

Xcode - 循环浏览以前打开的文档

xcode - 使用 UITableView 实现 UISearchController

ios - 角标(Badge)计数器的 APNS 后台处理

ios - cellForRowAtIndexPath 数据源方法的问题

ios - 如何在发送短信后手动返回应用程序

ios - 升级到 Xcode 4.2 beta 版本后,Jetbrains Appcode 是否仍然有效?

ios - iOS 7.0.3 中的条形半透明消失了