为什么我的 UITableViewController 没有填充在这里?

发布于 2024-12-05 16:06:19 字数 1909 浏览 1 评论 0原文

只是想了解当我的视图控制器出现在此处时我的表没有被填充。我正在使用 AIHTTPRequest 与 REST API 进行通信。该调用返回一个关联数组的数组(我假设我必须将其转换为 NSDictionaries 的 NSArray)。

在我的 TableViewController 的标头中:

@interface MyTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
    NSArray *homepageItems;
}

@property (nonatomic, retain) NSArray * homepageItems;

在实现中:

@synthesize homepageItems;
....
- (void)viewDidLoad {

NSURL *url = [NSURL URLWithString:URL_TO_MY_API];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];

当返回数据时:

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];
    homepageItems = (NSArray *)response;
}

在表视图方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
    }

    NSDictionary *dataItem = [homepageItems objectAtIndex:indexPath.row];

    // Configure the cell...
    cell.textLabel.text = [dataItem objectForKey:@"message"];

    return cell;
}

当视图加载时表中没有任何内容。我怀疑发生这种情况是因为当触发表视图方法时,异步方法尚未填充 homepageItems。如何实现将数据加载到表视图中?

谢谢,

编辑:在我的 requestFinished 方法中,我通过执行以下操作检查了 homepageItems 对象:

for(NSDictionary *dictionary in homepageMMDs) {
    NSLog(@"%@", [dictionary objectForKey:@"message"]);
}

并且正在记录来自我的 API 服务的正确消息。

Just trying to understand my table isn't being populated when my view controller appears here. I'm using AIHTTPRequest to communicated with a REST API. The call returns an array of associative arrays (which I'm assuming I'll have to cast to an NSArray of NSDictionaries).

In the header of my TableViewController:

@interface MyTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
    NSArray *homepageItems;
}

@property (nonatomic, retain) NSArray * homepageItems;

In the implementation:

@synthesize homepageItems;
....
- (void)viewDidLoad {

NSURL *url = [NSURL URLWithString:URL_TO_MY_API];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];

When the data is returned:

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];
    homepageItems = (NSArray *)response;
}

And in the Table View method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
    }

    NSDictionary *dataItem = [homepageItems objectAtIndex:indexPath.row];

    // Configure the cell...
    cell.textLabel.text = [dataItem objectForKey:@"message"];

    return cell;
}

When the view loads nothing is in the table. I suspect this is happening because when the table view method is fired homepageItems hasn't been populated by the asynchronous method. How can I achieve the data being loaded into the table view?

Thanks,

EDIT: In my requestFinished method, I've examined the homepageItems object by doing the following:

for(NSDictionary *dictionary in homepageMMDs) {
    NSLog(@"%@", [dictionary objectForKey:@"message"]);
}

And the proper messages from my API service are being logged.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

昇り龍 2024-12-12 16:06:19

您需要保留 homepageItems

 self.homepageItems = (NSArray *)response;

并重新加载 tableview,如 Joe 所说

You need to retain the homepageItems

 self.homepageItems = (NSArray *)response;

And also reload the tableview as Joe said

泪之魂 2024-12-12 16:06:19

尝试重新加载数据!

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];

    //Use self. to properly retain and you do not need to cast id assignments
    self.homepageItems = response; 

    //Make sure you reload the data!!
    [self.tableView reloadData];
}

Try reloading the data!

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];

    //Use self. to properly retain and you do not need to cast id assignments
    self.homepageItems = response; 

    //Make sure you reload the data!!
    [self.tableView reloadData];
}
狼亦尘 2024-12-12 16:06:19

您不会保留下载的数据:

而是:

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data.
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];
    homepageItems = (NSArray *)response;
}

您需要执行以下操作:(至关重要的是在 homepageItems 前面使用 self

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data.
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];
    self.homepageItems = (NSArray *)response; 
    // and others have said you need to reload the tableView
    [self.tableView reloadData];
}

You are not retaining the downloaded data:

Instead of:

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data.
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];
    homepageItems = (NSArray *)response;
}

You need to do: (crucially using self in front of homepageItems)

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data.
    NSString *responseString = [request responseString];
    id response = [responseString objectFromJSONString];
    self.homepageItems = (NSArray *)response; 
    // and others have said you need to reload the tableView
    [self.tableView reloadData];
}
梦里寻她 2024-12-12 16:06:19

您可以使用此 cocoapod https://github.com/xmartlabs/XLData 来从API 端点并填充 UITableView/UICollectionView。

You can use this cocoapod https://github.com/xmartlabs/XLData in order to fetch data from an API endpoint and populate a UITableView/UICollectionView.

长发绾君心 2024-12-12 16:06:19

我建议你创建一个显示你的 API 字符串的 php 页面,然后你可以解析元素并将它们导入到你想要的任何地方,这更容易!与你的代码相关,我想你需要解析元素,你可以在这里看到: http://www.ibm.com/developerworks/library/x-ioschat/
幸运的。

I suggest you to create an php page which displays your API strings,before you can parse the elements and import they to wherever you want,its much easier!Related to your code i suppose you need to parse the element,as you can se here : http://www.ibm.com/developerworks/library/x-ioschat/.
Lucky.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文