我的表格视图已创建,但数组未显示在表格视图中,控制台中的所有数据都来了。请帮我解决 json 解析
Posted
技术标签:
【中文标题】我的表格视图已创建,但数组未显示在表格视图中,控制台中的所有数据都来了。请帮我解决 json 解析【英文标题】:my table view is created but array is not showing in table view,in console all the data are coming.please help me to solve json parsing 【发布时间】:2016-03-02 07:37:15 【问题描述】:这是一个响应,我想将此响应值打印到表格视图中: 我正在发布我的 tableview 代码,请检查它是对还是错。 我在这里挣扎了2天,如果有人知道请回复我。
- (IBAction)search:(UIButton *)sender
NSString *post =[[NSString alloc]initWithFormat:@"keyword=%@",[keyword text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"url"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
//NSError *error = [[NSError alloc] init];
NSError *error=nil;
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!urlData)
NSLog(@"Error: %@", [error localizedDescription]);
//return NO;
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
NSError *error = nil;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingMutableContainers error:&error];
NSLog(@"json ==> %@ ", dic);
name = [[NSMutableArray alloc]init];
username = [[NSMutableArray alloc]init];
for (id obj in dic)
NSString *nameNameObj=[obj objectForKey:@"name"];
[name addObject:nameNameObj];
NSString *userNameObj=[obj objectForKey:@"username"];
[username addObject:userNameObj];
NSLog(@"name array is %@",name);
NSLog(@"username array is %@",username);
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// Return the number of sections.
return [name count];
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [name count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
static NSString *simpleTableIdentifier = @"MYCustomCell";
MyCustomCellTableViewCell *cell = (MyCustomCellTableViewCell *)
[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
NSArray *nib = [[NSBundle mainBundle]
loadNibNamed:@"MyCustomCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
// apply setters for cell
cell.nameLbl.text=[name objectAtIndex:indexPath.row];
cell.usernameLbl.text=[username objectAtIndex:indexPath.row];
return cell;
[
"id":60,"username":"test001","name":"name",
"id":61,"username":"test","name":"",
"id":65,"username":"test002","name":"first",
"id":69,"username":"test003","name":"second",
"id":71,"username":"test004","name":"third",
"id":78,"username":"test005","name":"fourth",
"id":79,"username":"test006","name":"Name of the person"
]
【问题讨论】:
请在此处添加您的 tableview 委托方法。没有它,我们无法帮助您。 【参考方案1】:忽略这个,因为你没有使用这个
检查这 2 行:
cell.nameLbl.text = [name objectAtIndex:indexPath.row];
cell.usernameLbl.text = [username objectAtIndex:indexPath.row];
name
是一个数组,在每个索引中都包含一个 NSDictionary。所以如果你想访问它的值,你必须这样做:
cell.nameLbl.text = [[name objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.usernameLbl.text = [[username objectAtIndex:indexPath.row] objectForKey:@"username"]];
编辑:
当您编辑了代码后,修改后的答案将是:
- (IBAction)search:(UIButton *)sender
NSString *post =[[NSString alloc]initWithFormat:@"keyword=%@",[keyword text]];
NSLog(@"PostData: %@",post);
......
......
if ([response statusCode] >=200 && [response statusCode] <300)
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
.....
.....
for (id obj in dic)
NSString *nameNameObj=[obj objectForKey:@"name"];
[name addObject:nameNameObj];
NSString *userNameObj=[obj objectForKey:@"username"];
[username addObject:userNameObj];
.......
.......
[self.tableView reloadData];
【讨论】:
不,它不工作。数据再次进入控制台但未显示在表格视图中.. @ParveshMishra > 你的名字和用户名数组有数据吗? 2016-03-02 15:38:23.107 UITableView1[7722:105442] 名称数组是 (vfvv, "", "", "Ben Franklin", vneflv, "Ben Franklin", "Name的人”) 2016-03-02 15:38:23.108 UITableView1[7722:105442] 用户名数组是 (test001, test, test002, test003, test004, test005, test006) cell.nameLbl.text =@"测试文本";检查你是否能看到这个。 所以你可以看到问题不在这里。问题出在其他地方。【参考方案2】:可能有以下一些原因:
确保正确设置委托和数据源。
当您收到响应时,在主线程上重新加载 tableView
dispatch_async(dispatch_get_main_queue(), ^
[self.tableView reloadData];
);
从
返回有效单元格cellForRowAtIndexPath:
返回有效的单元格数
numberOfRowInSection:
【讨论】:
以上是关于我的表格视图已创建,但数组未显示在表格视图中,控制台中的所有数据都来了。请帮我解决 json 解析的主要内容,如果未能解决你的问题,请参考以下文章