-1

解析后将响应存储在数组中。从该数组中,使用 valueForKey 选择键值对并存储在另一个数组中。如何在uitableview的每一行中显示json响应。我在一行中得到整个响应

arrTotalRequests=[[NSMutableArray alloc]init];

str= [[NSUserDefaults standardUserDefaults] valueForKey:@"user_id"];

NSString *strJson=[NSString stringWithFormat:@"userId=%@",str];
NSString *strlength=[NSString stringWithFormat:@"%d",[strJson length]];
NSURL *url=[NSURL URLWithString:@"url"];

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[request addValue:strlength forHTTPHeaderField:@"Content-Length"];
NSData *requestData=[strJson dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestData];
[request setHTTPMethod:@"POST"];
NSError *err;
NSData *responseData=[NSURLConnection  sendSynchronousRequest:request returningResponse:nil error:&err];

NSString *strResponse=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];


NSLog(@"response is %@",strResponse);
//        [[NSURLConnection alloc]initWithRequest:request delegate:self];
arr1=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&err];
arr2=[arr1 valueForKey:@"result"];
arrTotalRequests = [arr2 valueForKey:@"description"];
   }
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrTotalRequests count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}
NSString *str2=[NSString stringWithFormat:@"%@",  [arrTotalRequests valueForKey:@"description"]];
NSLog(@"new desc is %@",str2);
cell.textLabel.text=str2;
}
4

1 回答 1

1

尝试像这样使用..

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      return [arrTotalRequests count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *identifier=@"cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
     if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
    cell.textLabel.text=[[arrTotalRequests valueForKey:@"description"] objectAtIndex:indexPath.row];
}
于 2013-10-15T11:26:56.650 回答