在tableView中加載的數據是我的描述。在ViewDidLoad之後NSMutableArray爲空
- 我想從API中添加對象,它以JSON形式將數據反饋到NSMutableArray,但它不會使用tableview加載。
- 我可以看到數組設置好,但它不會加載到tableview,直到我嘗試滾動tableview。
這是非常煩人的,無法弄清楚爲什麼善意幫我弄清楚。
這裏是我的代碼 -
- (void)viewDidLoad {
[SVProgressHUD showWithStatus:@"Please wait ..."];
//get the bookings
[self getBookings];
[super viewDidLoad];
[self.tableView reloadData];
}
-(void) getBookings
{
[email protected]"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];
}
首先,您是否成功獲得服務器的響應? – user3182143
是的,我確實得到了迴應,並顯示了JSON數據。正如我所說,它顯示當我嘗試滾動tableview。 – Tajuddin