简体   繁体   English

在普通视图控制器中创建和填充表视图

[英]Creating and populating table view in normal view controller

I bet this question has been asked a thousand times, but I wasn't able to find one that directly relates to the problem I'm having right now, so I'd really appreciate some help from you guys. 我敢打赌这个问题已经问了上千遍了,但是我找不到一个与我现在遇到的问题直接相关的问题,所以我非常感谢你们的帮助。 Here goes: 开始:

I have a view controller in my iPhone application that, when triggered, requests a JSON string from a server. 我的iPhone应用程序中有一个视图控制器,该控制器在触发后会从服务器请求JSON字符串。 When the app receives the JSON it gets parsed, and a table view is populated with the corresponding data. 当应用程序接收到JSON时,将对其进行解析,并在表视图中填充相应的数据。 While this is taking place, I'm showing a UIActivityIndicatorView to indicate that something is going on behind the scenes. 发生这种情况时,我正在显示一个UIActivityIndicatorView来指示幕后正在发生某些事情。

I can download and parse the JSON just fine, but when it comes to populating the table view, I'm pretty lost. 我可以很好地下载和解析JSON,但是当涉及到填充表视图时,我很迷茫。 Since I have to display a completely white view and not an empty table view with an activity indicator on it, I haven't made my view controller a subclass of UITableViewController (which by default displays an empty list). 由于我必须显示一个完全白色的视图,而不是一个带有活动指示器的空表视图,因此我没有将视图控制器作为UITableViewController的子类(默认情况下显示一个空列表)。

How do I create and populate a table view with the data that I've downloaded from the server, and how do I do it without the normal cellForRowAtIndexPath and those kinds of methods? 如何使用从服务器下载的数据来创建和填充表视图,以及如何在没有常规的cellForRowAtIndexPathcellForRowAtIndexPath方法的情况下进行操作?

Thanks! 谢谢!

Solution

I just figured out how to solve my problem. 我只是想出了解决问题的方法。 When the server response has returned, I allocate my table view, set its datasource and delegate to self , then populate the table view with the returned data by implementing the same methods that a UITableViewController implements. 服务器响应返回后,我分配我的表视图,将其数据源设置为self ,然后通过实现与UITableViewController相同的方法,用返回的数据填充表视图。 Remember, as @Deepak points out, to adopt the UITableViewDatasource and UITableViewDelegate in the header file. 记住,正如@Deepak指出的那样,在头文件中采用UITableViewDatasourceUITableViewDelegate

I don't see why you can't use a UITableViewController subclass and do 我不明白为什么你不能使用UITableViewController子类来做

// Create UIActivityIndicatorView instance
[self.view addSubview:activityIndicatorView];

If you need an empty white view, you can also add it as a subview here. 如果需要空白视图,也可以在此处将其添加为子视图。

As such if you are interested in continuing in the current way. 这样,如果您有兴趣以当前方式继续。 You will need to have your UIViewController subclass adopt the UITableViewDatasource and UITableViewDelegate protocols and implement the appropriate methods. 您将需要让您的UIViewController子类采用UITableViewDatasourceUITableViewDelegate协议并实现适当的方法。 I don't think you can use a UITableView without doing that. 我认为您不这样做就不能使用UITableView

Edit 编辑

It is very strange that when you take out the NIB, this behaves differently. 很奇怪,当您取出NIB时,其表现会有所不同。 If you change the number of rows to even 1 , the overlay shows correctly. 如果将行数更改为偶数1 ,则覆盖图将正确显示。 This seems to be a bug. 这似乎是一个错误。

I suggest that you implement loadView to fix this where you separate self.view and self.tableView . 我建议您实现loadView来解决此问题,将self.viewself.tableView分开。

- (void)loadView {
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
    self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM