Can any one check with me this code i can't get json item in my table view, i only get them on the Xcode console and i can't find the error. ………………………………………………………………………………………………………………………………………………………………………………………………………………………………………...
/*
static NSString * const BaseURLStringg = @"http://api- public.guidebox.com/v1.43/Tunisia/rKgEWJbFg0kgEHrcGXPKhPDo0XtTafyC/movies /all/250/250";
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSMutableArray *tableData;
@property (weak, nonatomic) IBOutlet UITableView *tab;
*/
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:BaseURLStringg];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url.absoluteString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"JSON: %@", responseObject);
NSDictionary *jsonDict = (NSDictionary *) responseObject;
NSArray *products = [jsonDict objectForKey:@"results"];
[products enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop){
NSString *title= [NSString stringWithFormat:@"%@ ", [obj objectForKey:@"title"] ];
NSString *release_year = [NSString stringWithFormat:@"%@ ", [obj objectForKey:@"release_year"] ];
NSString *themoviedb= [NSString stringWithFormat:@"%@ ", [obj objectForKey:@"themoviedb"] ];
NSString *original_title= [NSString stringWithFormat:@"%@ ", [obj objectForKey:@"original_title"] ];
[_tab clearsContextBeforeDrawing];
[_tableData insertObject:title atIndex:0];
[_tableData insertObject:release_year atIndex:1];
[_tableData insertObject:themoviedb atIndex:2];
[_tableData insertObject:original_title atIndex:3];
}]; //[self tableData];
// [_tableData addObject:@"gggggggg"];
dispatch_sync(dispatch_get_main_queue(), ^{
//self.tableData=[[NSMutableArray alloc]initWithArray:responseObject];
[self.tab reloadData];
});
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", error);
}];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{
return [self.tableData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* mycell=[tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
// NSInteger nameindex=[_DB.arrColumnNames indexOfObject:@"name"];
//NSString* name=[[_myarray objectAtIndex:indexPath.row] objectAtIndex:nameindex];
if (_tableData!=nil){
UILabel *title = [mycell.contentView viewWithTag:101];
UILabel *release_year = [mycell.contentView viewWithTag:102];
UILabel *themoviedb = [mycell.contentView viewWithTag:103];
UILabel *original_title = [mycell.contentView viewWithTag:104];
title.text= [_tableData objectAtIndex:indexPath.row];
release_year.text= [_tableData objectAtIndex:indexPath.row];
themoviedb.text= [_tableData objectAtIndex:indexPath.row];
original_title.text= [_tableData objectAtIndex:indexPath.row];
}
//mycell.textLabel.text=@"eererr";
mycell.textLabel.textColor = [UIColor blackColor];
mycell.contentView.backgroundColor = [UIColor clearColor];
mycell.backgroundColor = [UIColor clearColor];
return mycell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
You are setting the data like so:
[_tableData insertObject:title atIndex:0];
[_tableData insertObject:release_year atIndex:1];
[_tableData insertObject:themoviedb atIndex:2];
[_tableData insertObject:original_title atIndex:3];
But you are reading out the data like:
title.text= [_tableData objectAtIndex:indexPath.row];
release_year.text= [_tableData objectAtIndex:indexPath.row];
themoviedb.text= [_tableData objectAtIndex:indexPath.row];
original_title.text= [_tableData objectAtIndex:indexPath.row];
indexPath.row will be the same integer (e.g. for the first cell in the section it will be 0). So for that cell the title will be set to title.text
, release_year.text
, themoviedb.text
, and original_title.text
. This is not what you intend. You should use an NSDictionary and add that to self.tableData. For example you should replace the first bit of code with:
NSDictionary *movieDictionary = @{@"title":title,@"release_year":release_year,@"themoviedb":themoviedb,@"original_title":original_title};
[self.tableData addObject:movieDictionary];
The in cellForRowAtIndexPath:
NSDictionary *movieDictionary = self.tableData[indexPath.row];
title.text= movieDictionary[@"title"];
release_year.text= movieDictionary[@"release_year"];
themoviedb.text= movieDictionary[@"themoviedb"];
original_title.text= movieDictionary[@"original_title"];