为什么我的 UITableViewController 没有填充在这里?
只是想了解当我的视图控制器出现在此处时我的表没有被填充。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要保留 homepageItems
并重新加载 tableview,如 Joe 所说
You need to retain the homepageItems
And also reload the tableview as Joe said
尝试重新加载数据!
Try reloading the data!
您不会保留下载的数据:
而是:
您需要执行以下操作:(至关重要的是在
homepageItems
前面使用self
)You are not retaining the downloaded data:
Instead of:
You need to do: (crucially using
self
in front ofhomepageItems
)您可以使用此 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.
我建议你创建一个显示你的 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.