简体   繁体   中英

my tableView can't get the list

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 (eg 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"];

I don't read all code, but this line I think may be wrong.

    dispatch_sync(dispatch_get_main_queue(), ^{
        //self.tableData=[[NSMutableArray alloc]initWithArray:responseObject];
        [self.tab reloadData];
    }); 

GCD write like this , will lock main thread, then UI refresh will not execute, if you want to reloadData , you need write like below

    dispatch_asyn(dispatch_get_main_queue(), ^{
        //self.tableData=[[NSMutableArray alloc]initWithArray:responseObject];
        [self.tab reloadData];
    }); 

Hope will help you.

try this type of code code

 NSURL *URL = [NSURL URLWithString:@"http://api.androidhive.info/songs/albums.php"];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        [manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            cource_data=[responseObject valueForKey:@"name"];
             NSLog(@"JSON: %@", cource_data);
            [self.tbl_cource reloadData];
        } failure:^(NSURLSessionTask *operation, NSError *error) {
            NSLog(@"Error: %@", error);
   }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM