如何在表中放置数组

时间:2016-01-26 19:28:29

标签: ios objective-c uitableview

你好朋友我有问题,解析一个数组,我想放入一个表, 表中没有填充这些信息给我带来了我的数组。 我可以帮忙解决这个问题,使用的代码如下。

BuscaViewController.m

-(void) searchBarSearchButtonClicked:(UISearchBar *)search{
NSInteger success = 0;
@try { 
    if([[self.search text] isEqualToString:@""]) {     
        [self alertStatus:@"No has ingresado ningun cambio" :@"" :0];      
    } else {
        NSString *post =[[NSString alloc] initWithFormat:@"&criterio=%@",[self.search text]];
        NSLog(@"PostData: %@",post);
        NSURL *url=[NSURL URLWithString:@"http://tacomander.com.mx/php/BuscarTaquerias.php"];
        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];

        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        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 *jsonData = [NSJSONSerialization
                                      JSONObjectWithData:urlData
                                      options:NSJSONReadingMutableContainers
                                      error:&error];

            success = [jsonData[@"success"] integerValue];
            NSLog(@"Success: %ld",(long)success);

            if(success == 1)
            {
                NSLog(@"Si esta bien");
                [self firParsear:urlData];
            } else {
                NSString *error_msg = (NSString *) jsonData[@"error_message"];
                [self alertStatus:error_msg :@"No se encontraron taquerias " :0];
            } 
        } else {
            //if (error) NSLog(@"Error: %@", error);
            [self alertStatus:@"Fallo la conexion" :@"" :0];
        }
    }
}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
    [self alertStatus:@"Busqueda Fallida." :@"Error!" :0];
}
}

控制台,如果我返回一个查询,因为我将这些数据放在表中。

- (void)firstParse:(NSData *)urlData
 {
NSError *error = nil;
NSDictionary *jsoDictionar = [NSJSONSerialization JSONObjectWithData:urlData
                                                        options:kNilOptions
                                                          error:&error];
// si hubo algún error en el parseo lo mostramos
if (error != nil)
{
    NSLog(@"ERROR: %@", [error localizedDescription]);
}
else {
    self.taquerias = [jsonDictionar objectForKey:@"Taquerias"];
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

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

 - (UITableViewCell *)tableView:(UITableView *)tableView    cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView   dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *taquero = [self.taquerias objectAtIndex:indexPath.row];
cell.textLabel.text = [taquero objectForKey:@"nombre"];

NSString *subtitle = [NSString stringWithFormat:@"Precio: %@ ",
                      [taquero objectForKey:@"idPrecio"]];
cell.detailTextLabel.text = subtitle;

return cell;
}

感谢您的时间和帮助

1 个答案:

答案 0 :(得分:0)

我认为问题出在这里

- (void)firstParse:(NSData *)urlData {
   NSError *error = nil;
   NSDictionary *jsoDictionar = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];

   // si hubo algún error en el parseo lo mostramos (parce trate de usar todo en ingles o lo llenan de negativos).
   if (error != nil)
   {
       NSLog(@"ERROR: %@", [error localizedDescription]);
   }
   else {
       self.taquerias = [jsonDictionar objectForKey:@"Taquerias"];
       [yourtableview reloadData]; ---> here Code.
   }
}