繁体   English   中英

如何使用目标c使用剩余api调用填充表视图单元格或控制器

[英]how to populate table view cell or controller with rest api call using objective c

https://github.com/svenanders/iOS7-Rest-Example-App

我尝试了这种编码...但是它不适用于我的网址...我的数据格式是这样的...

{"wk_times":[{"user":{"id":1,"name":"Redmine Admin"},"hours":11.0,"startdate":"2015-07-27","status":"n"},{"user":{"id":1,"name":"Redmine Admin"},"hours":42.0,"startdate":"2015-07-20","status":"n"}

我必须在表格视图单元格中显示此数据,例如:

开始日期| 状态| 用户名

谁能帮我...关于这个...我是ios开发的新手

现在只是为了解决您的问题,请按照以下步骤操作。 更好的是,您可以查看一些UITableView教程和JSON解析,并了解NSDictionaryNSArray 这将完全帮助您做事。

按照源代码。 在JSON序列化之后的fetchRestData中定义的完成处理程序块中。 如下所示:

NSDictionary *rest_data = [NSJSONSerialization JSONObjectWithData:data

在ViewController接口上创建一个NSArray,如下所示:

@property (nonatomic, strong) NSArray *users;

根据您在问题中提到的响应JSON,您需要将用户放入一个数组中,例如:

self.users = [NSArray arrayWithArray:[rest_data objectForKey:@"wk_times"]];

现在,您有了要在表视图中显示的用户列表。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return self.users.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     // Create cell to display the data.
     NSDictionary *userDict = self.users[indexPath.row];  // This userDict will have this {"user":{"id":1,"name":"Redmine Admin"},"hours":11.0,"startdate":"2015-07-27","status":"n"}
     NSDictionary *user = userDict[@"user"];   // This user will have {"id":1,"name":"Redmine Admin"}
     NSString *name = user[@"name"];
     NSString *userId = user[@"id"];
     // Using these data you set to the table cells.
}

这可能对您有帮助。

暂无
暂无

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

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