NSMutableArray 在 ViewDidLoad 之后为空
NSMutableArray is empty after ViewDidLoad
数据未加载到 tableView 中是我的描述。
- 我正在尝试将 API 中以 JSON 数据形式响应我的对象添加到 NSMutableArray 中,但它没有加载 tableview。
- 我可以看到数组设置正常,但在我尝试滚动 tableview 之前它不会加载到 tableview 中。
实在是太烦人了,想不通为什么好心人帮我搞清楚。
这是我的代码 -
- (void)viewDidLoad {
[SVProgressHUD showWithStatus:@"Please wait ..."];
//get the bookings
[self getBookings];
[super viewDidLoad];
[self.tableView reloadData];
}
-(void) getBookings
{
uid=@"41";
bookingsList=[[NSMutableArray alloc] init];
NSString *urlAsString = [NSString stringWithFormat:@"http://www.webservice.com/cleaning/api/booking/show/%@/%d",uid,0];
NSURL *url = [[NSURL alloc] initWithString:urlAsString];
NSLog(@"%@", urlAsString);
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"URL Session Task Failed: %@", [error localizedDescription]);
}
else {
NSArray *postsFromResponse = [NSJSONSerialization JSONObjectWithData: data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Count %d", postsFromResponse.count);
NSLog(@"JSON: %@", postsFromResponse);
//remove all objects from array
[self.bookingsList removeAllObjects];
for (NSDictionary *attributes in postsFromResponse) {
booking *bk = [[booking alloc] init];
[bk setAddress:[attributes objectForKey:@"Address"]];
[bk setBookingId:[attributes objectForKey:@"BookingID"]];
[bk setServiceDate:[attributes objectForKey:@"ServiceDate"]];
[bk setClientName:[attributes objectForKey:@"ClientName"]];
[bk setStatus:[attributes objectForKey:@"Status"]];
[bk setServiceTime:[attributes objectForKey:@"ServiceTime"]];
[bk setPrice:[attributes objectForKey:@"Price"]];
[bk setCleanType:[attributes objectForKey:@"CleanType"]];
[bk setNumOfHours:[attributes objectForKey:@"NumOfHours"]];
//add to array
[self.bookingsList addObject:bk];
}
NSLog(@"Records found -%lu",(unsigned long)[bookingsList count]);
[self.tableView reloadData];
if(bookingsList.count==0)
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"No Data Alert!"
message:@"No bookings found!" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
//[alertView show];
[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
}
}
}];
[SVProgressHUD dismiss];
}
#pragma mark - TableView Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Total rows - %lu", (unsigned long)[self.bookingsList count]);
return [self.bookingsList count];
}
使用它会对你有所帮助
- (void)viewDidLoad {
[SVProgressHUD showWithStatus:@"Please wait ..."];
//get the bookings
[self getBookings];
[super viewDidLoad];
}
-(void) getBookings
{
uid=@"41";
bookingsList=[[NSMutableArray alloc] init];
NSString *urlAsString = [NSString stringWithFormat:@"http://www.webservice.com/cleaning/api/booking/show/%@/%d",uid,0];
NSURL *url = [[NSURL alloc] initWithString:urlAsString];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:urlAsString];
[request setHTTPMethod:GET_REQUEST];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *err)
{
if (error) {
NSLog(@"URL Session Task Failed: %@", [error localizedDescription]);
}
else {
NSArray *postsFromResponse = [NSJSONSerialization JSONObjectWithData: data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Count %d", postsFromResponse.count);
NSLog(@"JSON: %@", postsFromResponse);
//remove all objects from array
[self.bookingsList removeAllObjects];
for (NSDictionary *attributes in postsFromResponse) {
booking *bk = [[booking alloc] init];
[bk setAddress:[attributes objectForKey:@"Address"]];
[bk setBookingId:[attributes objectForKey:@"BookingID"]];
[bk setServiceDate:[attributes objectForKey:@"ServiceDate"]];
[bk setClientName:[attributes objectForKey:@"ClientName"]];
[bk setStatus:[attributes objectForKey:@"Status"]];
[bk setServiceTime:[attributes objectForKey:@"ServiceTime"]];
[bk setPrice:[attributes objectForKey:@"Price"]];
[bk setCleanType:[attributes objectForKey:@"CleanType"]];
[bk setNumOfHours:[attributes objectForKey:@"NumOfHours"]];
//add to array
[self.bookingsList addObject:bk];
}
NSLog(@"Records found -%lu",(unsigned long)[bookingsList count]);
dispatch_async(dispatch_get_main_queue(),^{
[self.tableView reloadData];
});
if(bookingsList.count==0)
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"No Data Alert!"
message:@"No bookings found!" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
//[alertView show];
[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
}
}
}];
[task resume];
[SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Total rows - %lu", (unsigned long)[self.bookingsList count]);
return [self.bookingsList count];
}
编码约定:任何 属性 的支持变量都应以下划线开头。就像 _bookingsList。如果您不遵循该连接,则在没有您的完整源代码的情况下我们无法知道对 bookingsList 的分配将存储结果的位置。
数据未加载到 tableView 中是我的描述。
- 我正在尝试将 API 中以 JSON 数据形式响应我的对象添加到 NSMutableArray 中,但它没有加载 tableview。
- 我可以看到数组设置正常,但在我尝试滚动 tableview 之前它不会加载到 tableview 中。
实在是太烦人了,想不通为什么好心人帮我搞清楚。
这是我的代码 -
- (void)viewDidLoad {
[SVProgressHUD showWithStatus:@"Please wait ..."];
//get the bookings
[self getBookings];
[super viewDidLoad];
[self.tableView reloadData];
}
-(void) getBookings
{
uid=@"41";
bookingsList=[[NSMutableArray alloc] init];
NSString *urlAsString = [NSString stringWithFormat:@"http://www.webservice.com/cleaning/api/booking/show/%@/%d",uid,0];
NSURL *url = [[NSURL alloc] initWithString:urlAsString];
NSLog(@"%@", urlAsString);
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"URL Session Task Failed: %@", [error localizedDescription]);
}
else {
NSArray *postsFromResponse = [NSJSONSerialization JSONObjectWithData: data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Count %d", postsFromResponse.count);
NSLog(@"JSON: %@", postsFromResponse);
//remove all objects from array
[self.bookingsList removeAllObjects];
for (NSDictionary *attributes in postsFromResponse) {
booking *bk = [[booking alloc] init];
[bk setAddress:[attributes objectForKey:@"Address"]];
[bk setBookingId:[attributes objectForKey:@"BookingID"]];
[bk setServiceDate:[attributes objectForKey:@"ServiceDate"]];
[bk setClientName:[attributes objectForKey:@"ClientName"]];
[bk setStatus:[attributes objectForKey:@"Status"]];
[bk setServiceTime:[attributes objectForKey:@"ServiceTime"]];
[bk setPrice:[attributes objectForKey:@"Price"]];
[bk setCleanType:[attributes objectForKey:@"CleanType"]];
[bk setNumOfHours:[attributes objectForKey:@"NumOfHours"]];
//add to array
[self.bookingsList addObject:bk];
}
NSLog(@"Records found -%lu",(unsigned long)[bookingsList count]);
[self.tableView reloadData];
if(bookingsList.count==0)
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"No Data Alert!"
message:@"No bookings found!" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
//[alertView show];
[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
}
}
}];
[SVProgressHUD dismiss];
}
#pragma mark - TableView Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Total rows - %lu", (unsigned long)[self.bookingsList count]);
return [self.bookingsList count];
}
使用它会对你有所帮助
- (void)viewDidLoad {
[SVProgressHUD showWithStatus:@"Please wait ..."];
//get the bookings
[self getBookings];
[super viewDidLoad];
}
-(void) getBookings
{
uid=@"41";
bookingsList=[[NSMutableArray alloc] init];
NSString *urlAsString = [NSString stringWithFormat:@"http://www.webservice.com/cleaning/api/booking/show/%@/%d",uid,0];
NSURL *url = [[NSURL alloc] initWithString:urlAsString];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:urlAsString];
[request setHTTPMethod:GET_REQUEST];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *err)
{
if (error) {
NSLog(@"URL Session Task Failed: %@", [error localizedDescription]);
}
else {
NSArray *postsFromResponse = [NSJSONSerialization JSONObjectWithData: data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Count %d", postsFromResponse.count);
NSLog(@"JSON: %@", postsFromResponse);
//remove all objects from array
[self.bookingsList removeAllObjects];
for (NSDictionary *attributes in postsFromResponse) {
booking *bk = [[booking alloc] init];
[bk setAddress:[attributes objectForKey:@"Address"]];
[bk setBookingId:[attributes objectForKey:@"BookingID"]];
[bk setServiceDate:[attributes objectForKey:@"ServiceDate"]];
[bk setClientName:[attributes objectForKey:@"ClientName"]];
[bk setStatus:[attributes objectForKey:@"Status"]];
[bk setServiceTime:[attributes objectForKey:@"ServiceTime"]];
[bk setPrice:[attributes objectForKey:@"Price"]];
[bk setCleanType:[attributes objectForKey:@"CleanType"]];
[bk setNumOfHours:[attributes objectForKey:@"NumOfHours"]];
//add to array
[self.bookingsList addObject:bk];
}
NSLog(@"Records found -%lu",(unsigned long)[bookingsList count]);
dispatch_async(dispatch_get_main_queue(),^{
[self.tableView reloadData];
});
if(bookingsList.count==0)
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"No Data Alert!"
message:@"No bookings found!" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
//[alertView show];
[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
}
}
}];
[task resume];
[SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Total rows - %lu", (unsigned long)[self.bookingsList count]);
return [self.bookingsList count];
}
编码约定:任何 属性 的支持变量都应以下划线开头。就像 _bookingsList。如果您不遵循该连接,则在没有您的完整源代码的情况下我们无法知道对 bookingsList 的分配将存储结果的位置。