Ios 我能';我无法从JSON获取要加载到UITableView的数组
.h包含Ios 我能';我无法从JSON获取要加载到UITableView的数组,ios,objective-c,json,uitableview,Ios,Objective C,Json,Uitableview,.h包含 @interface HomeTVC : UITableViewController @property (nonatomic, strong) NSArray *spotTitle; 而.m包含 #import "HomeTVC.h" @interface HomeTVC () @end @implementation HomeTVC - (id)initWithStyle:(UITableViewStyle)style { self = [super initWi
@interface HomeTVC : UITableViewController
@property (nonatomic, strong) NSArray *spotTitle;
而.m包含
#import "HomeTVC.h"
@interface HomeTVC ()
@end
@implementation HomeTVC
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
这是我发出服务器请求并获取数据的地方
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *homeDataURL = [NSURL URLWithString:@"XXXXX"];
NSData *jsonSpots = [NSData dataWithContentsOfURL:homeDataURL];
NSDictionary *spotTableDictionary = [NSJSONSerialization JSONObjectWithData:jsonSpots options:0 error:NULL];
NSArray *spotTitle = [spotTableDictionary valueForKey:@"title"];
NSLog(@"%@",spotTitle);
该NSLog
打印我要传递到终端中tableview中的数组,这里的一切似乎都正常工作
我的问题从这里开始:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.spotTitle count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.spotTitle objectAtIndex:indexPath.row];
return cell;
}
我一直在尝试将传递到NSLog语句的数组连接到我的tableview,但我不确定此时我做错了什么
dataSource
和delegate
都连接到主情节提要中的HomeTVC
程序编译和运行时没有错误或警告,并加载空的tableview
也许我遗漏了一些超基本的东西(我对编程非常陌生),这是我第一次尝试传入外部数据库。既然可以使用属性,为什么还要创建另一个变量。
NSArray *spotTitle = [spotTableDictionary valueForKey:@"title"];
self.spotTitle = spotTitle; //Do you forget assign to your property?
NSLog(@"%@",self.spotTitl);
就这么做吧:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *homeDataURL = [NSURL URLWithString:@"XXXXX"];
NSData *jsonSpots = [NSData dataWithContentsOfURL:homeDataURL];
NSDictionary *spotTableDictionary = [NSJSONSerialization JSONObjectWithData:jsonSpots options:0 error:NULL];
self.spotTitle = [spotTableDictionary valueForKey:@"title"];
NSLog(@"%@",self.spotTitle);
那应该能解决你的问题。属性和局部变量也不要使用相同的名称,这会在处理时导致不必要的混淆。既然可以使用属性,为什么还要创建另一个变量。 就这么做吧:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *homeDataURL = [NSURL URLWithString:@"XXXXX"];
NSData *jsonSpots = [NSData dataWithContentsOfURL:homeDataURL];
NSDictionary *spotTableDictionary = [NSJSONSerialization JSONObjectWithData:jsonSpots options:0 error:NULL];
self.spotTitle = [spotTableDictionary valueForKey:@"title"];
NSLog(@"%@",self.spotTitle);
那应该能解决你的问题。属性和局部变量也不要使用相同的名称,这会在处理时导致不必要的混淆。既然可以使用属性,为什么还要创建另一个变量。 就这么做吧:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *homeDataURL = [NSURL URLWithString:@"XXXXX"];
NSData *jsonSpots = [NSData dataWithContentsOfURL:homeDataURL];
NSDictionary *spotTableDictionary = [NSJSONSerialization JSONObjectWithData:jsonSpots options:0 error:NULL];
self.spotTitle = [spotTableDictionary valueForKey:@"title"];
NSLog(@"%@",self.spotTitle);
那应该能解决你的问题。属性和局部变量也不要使用相同的名称,这会在处理时导致不必要的混淆。既然可以使用属性,为什么还要创建另一个变量。 就这么做吧:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *homeDataURL = [NSURL URLWithString:@"XXXXX"];
NSData *jsonSpots = [NSData dataWithContentsOfURL:homeDataURL];
NSDictionary *spotTableDictionary = [NSJSONSerialization JSONObjectWithData:jsonSpots options:0 error:NULL];
self.spotTitle = [spotTableDictionary valueForKey:@"title"];
NSLog(@"%@",self.spotTitle);
那应该能解决你的问题。另外,不要对属性和局部变量使用相同的名称,这会在处理时导致不必要的混淆