0

我有 2 个控制器,1 个是从 JSON 加载数据,另一个是一个简单的 UITableViewController。我的问题是视图是在数据之前加载的。如何在我的表格之前加载数据?我是 Objective-C OOP 的新手:/

编码

#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
4

3 回答 3

2

在 MVC 中,通常每个视图都有一个视图控制器。因此,单个视图控制器将负责在一个视图上显示的所有内容。当然,您可以拥有辅助类,或使用子视图控制器(例如小部件),或者如果您正在为 iPad 开发,您可能有两个视图控制器(一个用于侧菜单,一个用于主视图)。

我建议您执行以下操作

  1. 使您的 DataController 成为一个子类,NSObject而不是UIViewController. 据我了解,它是一个数据访问类,不负责 UI。
  2. viewDidLoad您的 MasterViewController 的方法中,分配和初始化一个DataController对象并触发其数据加载方法。
  3. 设置回调,以便在获取数据时,使用数据调用MasterViewController上的方法。或者将 MasterViewController 设置为 DataController 的委托,完成后,将数据分配给 MasterViewController 的属性。
于 2013-03-31T17:00:50.690 回答
0

我猜你需要在初始化你的 TableViewController 之前执行数据加载,当你得到响应数据时加载 TableViewController。为此,请参阅这篇文章如何等到某个方法完成执行,在该方法中执行数据加载。

于 2013-03-31T16:56:06.540 回答
0

在加载数据时显示带有活动指示器覆盖的空白表格视图是可以的。数据加载完成后,您可以使用数据重新加载 tableview 并删除活动指示器。[tableview reloaddata] 将调用所有数据源委托方法,然后将您的数据包含在其中。

于 2013-03-31T16:59:46.593 回答