2013-03-31 33 views
0

我有2個控制器,1是從JSON加載數據,另一個是簡單的UITableViewController。我的問題是視圖在數據之前加載。我如何在我的表之前加載數據?我是新來的Objective-C OOP:/在ViewController之前加載DataController iOS

代碼

#import "MasterViewController.h" 

#import "DetailViewController.h" 

#import "DealsDataController.h" 

#import "Deals.h" 


@implementation MasterViewController 

- (void)awakeFromNib 
{ 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
     self.clearsSelectionOnViewWillAppear = NO; 
     self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); 
    } 
    [super awakeFromNib]; 

    self.dataController = [[DealsDataController alloc] init]; 
    NSLog(@"this is the awake from nib count %i",[self.dataController countOfList]); 

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view, typically from a nib. 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table View 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"did this count the list %i",[self.dataController countOfList]); 
    return [self.dataController countOfList]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"DealCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    Deals *dealAtIndex = [self.dataController objectInListAtIndex:indexPath.row]; 
    [[cell textLabel] setText:dealAtIndex.name]; 

    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return NO; 
} 


/* 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [_objects removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
     NSDate *object = _objects[indexPath.row]; 
     self.detailViewController.detailItem = object; 
    } 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showDetail"]) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     NSDate *object = _objects[indexPath.row]; 
     [[segue destinationViewController] setDetailItem:object]; 
    } 
} 

*/ 
@end 

和數據控制器

#import "DealsDataController.h" 
#import "Deals.h" 

@implementation DealsDataController 
@synthesize masterDealsList = _masterDealsList; 

-(id)init { 
    if (self = [super init]) { 
     [self initializeDataList]; 
     return self; 
    } 
    return nil; 
} 

-(NSMutableArray *)masterDealsList { 
    if (!_masterDealsList) { 
     _masterDealsList = [[NSMutableArray alloc] init]; 
    } 
    return _masterDealsList; 
} 

-(void)setMasterDealsList:(NSMutableArray *)newList { 
    if (_masterDealsList != newList) { 
     _masterDealsList = newList; 
    } 
} 

- (void)fetchedData:(NSData *)responseData { 
    //parse out the json data 
    NSError* error; 
    NSDictionary *json = [NSJSONSerialization 
          JSONObjectWithData:responseData //1 
          options:kNilOptions 
          error:&error]; 

    _deals = [json objectForKey:@"deals"]; //2 

    NSLog(@"deals: %@", _deals); 
    NSArray *dealName = [_deals valueForKey:@"deal_name"]; 
    NSLog(@"deals Name: %@", dealName); 

    for (int i = 1;i <= [_deals count]; i++) { 
    NSDictionary* dict = [_deals objectAtIndex:i-1]; 
    Deals *deal =[[Deals alloc] initWithProdID:1 name:[dict valueForKey:@"deal_name"] description:[dict objectForKey:@"deal_description"] price:10.00 specs:@"specs" terms:@"terms"]; 
    [self addDealWithDeal:deal]; 

    NSLog (@"masterListCount %i ", [self countOfList]); 

    } 

} 

-(void)initializeDataList { 
    //NSDate *today = [NSDate date]; 
    dispatch_async(sweetDealsQueue, ^{ 
     NSData* data = [NSData dataWithContentsOfURL:sweetDealsURL]; 
     [self performSelectorOnMainThread:@selector(fetchedData:) 
           withObject:data waitUntilDone:YES]; 
    }); 

} 

-(NSUInteger)countOfList { 

    return [self.masterDealsList count]; 

} 

-(Deals *)objectInListAtIndex:(NSUInteger)theIndex { 

    return [self.masterDealsList objectAtIndex:theIndex]; 
} 

-(void)addDealWithDeal:(Deals *)deal { 

     [self.masterDealsList addObject:deal]; 
} 


@end 
+0

我想通了......我所要做的就是將dispatch_async更改爲dispatch_sync ...它不會等待我的數據加載異步...這是有道理的。 – vinylDeveloper

回答

2

在MVC中,通常每個視圖都有一個視圖控制器。因此,單個視圖控制器將負責在一個視圖上顯示的所有內容。當然,你可以有助手類,或使用子視圖控制器(例如小工具),或者如果你正在爲iPad開發,你可能有兩個視圖控制器(一個用於側面菜單,一個用於主視圖)。

我會建議你做以下

  1. 讓您DataController類的NSObject而不是UIViewController一個子類。據我所知,它是一個數據訪問類,不負責UI。
  2. 在您的MasterViewController的viewDidLoad方法中,分配並初始化一個DataController對象並觸發其數據加載方法。
  3. 要麼設置回調,所以當數據被提取時,數據將調用MasterViewController上的方法。或者將您的MasterViewController設置爲您的DataController的代理,並在完成後將這些數據分配給MasterViewController的一個屬性。
+1

謝謝,我喜歡你的回答......特別是因爲我已經完成了前兩點,很高興聽到我走在正確的道路上。歡呼聲 – vinylDeveloper

+0

不客氣,很高興它適合你@vinylDeveloper –

0

我猜你需要初始化你TableViewController之前,當你執行數據加載獲取響應數據加載TableViewController。那refer this post如何等待某個方法完成執行,在那個方法中執行數據加載。

0

可以在數據加載時顯示帶有活動指示符疊加層的空白表格視圖。數據加載完成後,您可以重新加載tableview和數據並刪除活動指示器。 [tableview reloaddata]會調用所有的數據源委託方法,然後將其中的數據。

相關問題