我是IOS新手我需要将 json 提取的数据显示到 uitableview 我的json模式看起来像这样。
{"output":[{"id":{"php":"1"},"name":{"php":"ramkumar"},"image":{"1":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}},{"id":{"php":"2"},"name":{"php":"rakshi"},"image":{"2":"http:\/\/www.easyteach.gr\/users\/tutor\/1440371842.jpg"}},{"id":{"android":"1"},"name":{"android":"Vijayan"},"image":{"3":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}},{"id":{"android":"2"},"name":{"android":"vishali"},"image":{"4":"http:\/\/www.easyteach.gr\/users\/tutor\/1440371842.jpg"}},{"id":{"android":"3"},"name":{"android":"Niranjan"},"image":{"5":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}}]}
viewdidload 编码:
我在viewdidload中创建了json url到数组格式并在nslog中成功显示但在tableview中它可能失败。
if(data){
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
NSLog(@"%@",json);
nameArray = [json valueForKeyPath:@"output.name"];
php = [nameArray valueForKey:@"php"];
}
[_tabledata reloadData];
Tableview代表:
-(NSInteger)numberOfSectionsInTableView:(UITableView *) tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{
return [php count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];
cell.iteam.text=[php objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView: (UITableView *) tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
}
答案 0 :(得分:0)
这是一个奇怪的问题,但这里有一些指示。
首先,你没有提到php
变量是什么,但我认为它是NSArray
。在将其发送到UITableView
之前,可能需要检查您期望显示的元素数量:
php = [nameArray valueForKey:@"php"];
NSLog(@"There are %d items to be displayed", (int)php.count);
接下来,几年前我遇到了一个问题:UITableView
没有触发我的各种UITableView委托函数。
Have a read of this article看看我需要做什么,并尝试在代理功能上添加一些断点,看看他们是否被调用。
最后,您的cellForRowAtIndexPath
函数似乎每次都会创建一个新的TableViewCell
项:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];
cell.iteam.text=[php objectAtIndex:indexPath.row];
return cell;
}
如果操作系统无法重用现有对象,那么应该有一些代码只创建一个新单元格:
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];
if (cell == nil) {
cell = TableViewCell *cell=[[CompanyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"table"];
}
我也很想插入一行代码,设置默认 textLabel,看看是否有任何内容:
cell.textLabel.font = [php objectAtIndex:indexPath.row];
希望其中一些有帮助...
更新
实际上,我认为你误解了你的JSON字符串。
您的JSON包含一个元素output
,它是一个记录数组。
所以(我没试过)我认为你应该像这样定义php
:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
php = [json valueForKey:@"output"];
应该为您的UITableView
提供数组。但是从那里开始,您需要访问每条记录中的相关元素。
{
"id": {
"php": "1"
},
"name": {
"php": "ramkumar"
},
"image": {
"1": "http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"
}
}
目前,您的cellForRowAtIndexPath
功能会将text
设置为整个记录:
cell.iteam.text=[php objectAtIndex:indexPath.row];
答案 1 :(得分:-1)
使用以下代码行。希望它会对你有所帮助:
if(data){
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
NSLog(@"%@",json);
nameArray = [json valueForKeyPath:@"output.name"];
php = [[nameArray valueForKey:@"php"] mutableCopy];
[php removeObjectIdenticalTo:[NSNull null]];
}
[_tabledata reloadData];