Arie Prasetyo
Arie Prasetyo

Reputation: 151

Error Drawing JSOn into UITableviewCell

I have json like this (have dictionary and array). I want draw all json into UItableviewcell, but its not draw there

{   data: [
      {

        featured: true,
        price: {
                    currency: "IDR",
                    amount: 5557679,
                    formatted: "Rp5.558.000"
      },
] }

and this is my viewcontroller.h file

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UITableView *mainTableView;
    NSURL *URL;
    NSDictionary *Deals;
    NSArray *Deals_array;
    NSMutableData *data;
}

@end

and this is my .m file

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"SOme Apps";
    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
    URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://someurl.com"]];
    NSURLRequest *request=[NSURLRequest requestWithURL:URL];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    /*dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: URL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });*/
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    data=[[NSMutableData alloc] init];
    NSLog(@"%@",data);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];

}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSError* error;
    Deals= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    Deals_array = [Deals objectForKey:@"data"]; //2
    NSDictionary* loan = [Deals_array objectAtIndex:0];
    NSString *test=[loan objectForKey:@"featured"];
    NSLog(@"%@",test);
    [mainTableView reloadData];




}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"Error");
}

////set minimum section oftableview
-(int)numberOfTableviewSection : (UITableView *) tableView
{
    return 1;
}

////set length uitableview
-(int) tableView :(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [Deals_array count];
}

///declaring uitableview
-(UITableViewCell *)tableView :(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell==nil){
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }
    cell.textLabel.text=[[Deals_array objectAtIndex:indexPath.row] objectForKey:@"featured"];
    return  cell;
}
 @end

but its not draw the objectkey @test, can someone help me why?

Upvotes: 0

Views: 111

Answers (1)

Neo
Neo

Reputation: 2807

Try doing the allocation

Deals_array = [[NSArray alloc] initWithArray:[Deals objectForKey:@"data"]];

Upvotes: 1

Related Questions