【问题标题】:TableViewController content not displaying content (reloadData)TableViewController 内容不显示内容(reloadData)
【发布时间】: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


【解决方案1】:

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

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

所以拿出来

[self.tableView reloadData];

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

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

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

【讨论】:

  • 谢谢@beeef。我要试试,我希望它有效;)问候。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 2012-09-24
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
相关资源
最近更新 更多