0

任何人都可以和我一起检查这段代码吗?我无法在我的表格视图中获取 json 项目,我只能在 Xcode 控制台上获取它们并且我找不到错误。………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………

/*
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
4

3 回答 3

0

您正在像这样设置数据:

        [_tableData insertObject:title atIndex:0];
        [_tableData insertObject:release_year atIndex:1];
        [_tableData insertObject:themoviedb atIndex:2];
        [_tableData insertObject:original_title atIndex:3]; 

但是您正在读取如下数据:

        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 将是相同的整数(例如,对于该部分中的第一个单元格,它将是 0)。因此,对于该单元格,标题将设置为title.textrelease_year.textthemoviedb.textoriginal_title.text。这不是你想要的。您应该使用 NSDictionary 并将其添加到 self.tableData。例如,您应该将第一段代码替换为:

        NSDictionary *movieDictionary = @{@"title":title,@"release_year":release_year,@"themoviedb":themoviedb,@"original_title":original_title};
        [self.tableData addObject:movieDictionary];

在 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"];
于 2016-04-19T01:25:05.067 回答
0

我没有阅读所有代码,但我认为这一行可能是错误的。

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

GCD这样写,会锁定主线程,然后UI刷新不会执行,如果你想reloadData,你需要像下面这样写

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

希望能帮到你。

于 2016-04-19T03:12:57.233 回答
0

试试这种类型的代码代码

 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);
   }];
于 2016-04-19T03:18:00.493 回答