【问题标题】:How to populate the UITableView contents for the first time?第一次如何填充 UITableView 内容?
【发布时间】:2015-11-12 18:48:45
【问题描述】:

我在ViewController 中有一个UITableView。我第一次运行我的应用程序时,表是空的。当我浏览其他视图并返回视图(有一个 uitable)时,表格已填满。我使用以下解决方案,但它不起作用。我将这部分代码放在ViewWillAppear 和ViewDidLoad 中,但这不起作用。

dispatch_async(dispatch_get_main_queue(), ^{
        [self getthetablecontentfromserver];
        [self.tableView reloadData]; // to reload selected cell

    });

任何人都知道如何第一次填充表格的内容? 这里是 getthetablecontentfromserver 方法:

-(void)getthetablecontentfromserver{
    NSURL *url = [NSURL URLWithString:@"http://exampledomain.com/data"];
    config = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
    [[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        data= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [self.tableView reloadData];
    }
      ] resume];
}

【问题讨论】:

  • 你有调用任何网络服务吗?
  • @NimitParekh 是的,我正在从网络服务加载我的数据
  • 尝试将该代码放入 viewDidappear 方法
  • @Mina 收到数据后,只需重新加载表,如 [self.tblView reloadData];
  • @NimitParekh 我已经在 getthetablecontentfromserver 方法中收到我的数据后调用了重新加载数据。

标签: ios uitableview reload


【解决方案1】:

您应该在后台线程上调用getthetablecontentfromserver。完成后将NSNotification 发送回viewController 以调用reloadData。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多