我的 tableView 无法获取列表
my tableView can't get the list
任何人都可以和我一起检查这个代码我无法在我的 table 视图中获得 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
您正在这样设置数据:
[_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.text
、release_year.text
、themoviedb.text
和 original_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"];
我没有阅读所有代码,但我认为这一行可能是错误的。
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];
});
希望对你有所帮助。
尝试这种类型的代码代码
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);
}];
任何人都可以和我一起检查这个代码我无法在我的 table 视图中获得 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
您正在这样设置数据:
[_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.text
、release_year.text
、themoviedb.text
和 original_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"];
我没有阅读所有代码,但我认为这一行可能是错误的。
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];
});
希望对你有所帮助。
尝试这种类型的代码代码
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);
}];