<pre>
products = (
{
id = 19;
"image_url" = "http://localhost:8888/straightoffer/image/data/1330374078photography.jpg";
name = "Save $240 on your next photo session";
},
{
id = 21;
"image_url" = "http://localhost:8888/straightoffer/image/data/1330373696massage.jpg";
name = "One Hour Massage";
}
);
}
</pre>
Run Code Online (Sandbox Code Playgroud)
以上是我通过json得到的,我需要将值分配给uitableviews:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"行数:%d",[[products objectForKey:@"products"] count]);
return [[products objectForKey:@"products"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
如果(单元格 == 零){
单元格 = [[UITableViewCell 分配]
initWithStyle:UITableViewCellStyleDefault
重用标识符:@"单元格"];
}
NSString *currentProductName;
currentProductName = [productKeys objectAtIndex:indexPath.row];
NSLog(@"产品名称:%@", currentProductName);
[[cell textLabel] setText:currentProductName];
返回单元格;
}
它返回 0 行数,我是 ios 的新手,请帮助我如何将这些值分配给 uitableview。
问候
问题是你发布的不是json。它应该是
{
"products":[
{
"id":19,
"image_url":"http://localhost:8888/straightoffer/image/data/1330374078photography.jpg",
"name":"Save $240 on your next photo session"
},
{
"id":21,
"image_url":"http://localhost:8888/straightoffer/image/data/1330373696massage.jpg",
"name":"One Hour Massage"
}
]
}
Run Code Online (Sandbox Code Playgroud)
您可以使用http://jsonlint.com/来验证您的 json 文件。
我使用了上面的 json 文件并执行了以下操作:
NSBundle* bundle = [NSBundle mainBundle];
NSString *jsonPath = [bundle pathForResource:@"data" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *error;
NSDictionary *products = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
DLog(@"products: %i", [[products objectForKey:@"products"] count] );
[self.tableView reloadData];
Run Code Online (Sandbox Code Playgroud)
结果是:产品:2。
您应该能够重现这一点。
| 归档时间: |
|
| 查看次数: |
369 次 |
| 最近记录: |