为什么在init方法之前调用UITableViewController numberOfRowsInSections?

时间:2013-08-13 22:39:58

标签: uitableview parse-platform

我的UITableViewController不会加载我从Parse下载的信息。我知道链接正在工作,因为当我要求它打印出PFQuery时,我得到了我想要的结果。但是,我的问题是在init方法之前调用方法numberOfRowsInSection。我真的不确定如何在init方法之前下载我的数据。

这是我的.m文件

@implementation PicklerTable

@synthesize picklersArray;

- (id) init
{
     // Call the cuperclass's designated init
    self = [super initWithStyle:UITableViewStyleGrouped];

if(self){

    // Insert initial data here
    PFQuery *picklerQuery = [PFQuery queryWithClassName:@"Picklers"];

    [picklerQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            picklersArray = [[NSArray alloc] initWithArray:objects];                
        }
    }];

}

return self;
}


-(id) initWithStyle:(UITableViewStyle)style
{
return [self init];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return picklersArray.count;
}

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

UITableViewCell *cell = [[UITableViewCell alloc]     initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];

PFObject *picklerObject = [picklersArray objectAtIndex:indexPath.row];    
[[cell textLabel] setText:[picklerObject objectForKey:@"title"]];

return cell;
}

我在numberOfRowsInSection方法中放了一个断点,然后在控制台中键入bt。这是我的输出:

* thread #1: tid = 0x1c03, 0x00003430 TableControllerPractice`-[PicklerTable tableView:numberOfRowsInSection:](self=0x07c7d5a0, _cmd=0x01426163, tableView=0x08398a00, section=0) + 64 at PicklerTable.m:50, stop reason = breakpoint 1.1
frame #0: 0x00003430 TableControllerPractice`-[PicklerTable tableView:numberOfRowsInSection:](self=0x07c7d5a0, _cmd=0x01426163, tableView=0x08398a00, section=0) + 64 at PicklerTable.m:50
frame #1: 0x01074548 UIKit`-[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1822
frame #2: 0x01077224 UIKit`-[UITableViewRowData numberOfRows] + 98
frame #3: 0x00f3b952 UIKit`-[UITableView noteNumberOfRowsChanged] + 120
frame #4: 0x00f3b2dc UIKit`-[UITableView reloadData] + 769
frame #5: 0x00f3edd6 UIKit`-[UITableView _reloadDataIfNeeded] + 65
frame #6: 0x00f43a7e UIKit`-[UITableView layoutSubviews] + 36
frame #7: 0x00ee02dd UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 279
frame #8: 0x01d366b0 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
frame #9: 0x00424fc0 QuartzCore`-[CALayer layoutSublayers] + 240
frame #10: 0x0041933c QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 468
frame #11: 0x00424eaf QuartzCore`-[CALayer layoutIfNeeded] + 166
frame #12: 0x00f7f2bd UIKit`-[UIViewController window:setupWithInterfaceOrientation:] + 242
frame #13: 0x00ec7b56 UIKit`-[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5346
frame #14: 0x00ec666f UIKit`-[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
frame #15: 0x00ec6589 UIKit`-[UIWindow _setRotatableViewOrientation:duration:force:] + 89
frame #16: 0x00ec57e4 UIKit`__57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke_0 + 224
frame #17: 0x00ec561e UIKit`-[UIWindow _updateToInterfaceOrientation:duration:force:] + 209
frame #18: 0x00ec63d9 UIKit`-[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 853
frame #19: 0x00ec92d2 UIKit`-[UIWindow setDelegate:] + 351
frame #20: 0x00f7399c UIKit`-[UIViewController _tryBecomeRootViewControllerInWindow:] + 164
frame #21: 0x00ec0574 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 481
frame #22: 0x00ec076f UIKit`-[UIWindow _setHidden:forced:] + 368
frame #23: 0x00ec0905 UIKit`-[UIWindow _orderFrontWithoutMakingKey] + 49
frame #24: 0x00ec9917 UIKit`-[UIWindow makeKeyAndVisible] + 65
frame #25: 0x00002da6 TableControllerPractice`-[HomepwnerAppDelegate application:didFinishLaunchingWithOptions:](self=0x07c62cb0, _cmd=0x01426c21, application=0x0b05cb60, launchOptions=0x00000000) + 742 at HomepwnerAppDelegate.m:33
frame #26: 0x00e8d157 UIKit`-[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 266
frame #27: 0x00e8d747 UIKit`-[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1248
frame #28: 0x00e8e94b UIKit`-[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 805
frame #29: 0x00e9fcb5 UIKit`-[UIApplication handleEvent:withNewEvent:] + 1022
frame #30: 0x00ea0beb UIKit`-[UIApplication sendEvent:] + 85
frame #31: 0x00e92698 UIKit`_UIApplicationHandleEvent + 9874
frame #32: 0x0292bdf9 GraphicsServices`_PurpleEventCallback + 339
frame #33: 0x0292bad0 GraphicsServices`PurpleEventCallback + 46
frame #34: 0x01e72bf5 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
frame #35: 0x01e72962 CoreFoundation`__CFRunLoopDoSource1 + 146
frame #36: 0x01ea3bb6 CoreFoundation`__CFRunLoopRun + 2118
frame #37: 0x01ea2f44 CoreFoundation`CFRunLoopRunSpecific + 276
frame #38: 0x01ea2e1b CoreFoundation`CFRunLoopRunInMode + 123
frame #39: 0x00e8e17a UIKit`-[UIApplication _run] + 774
frame #40: 0x00e8fffc UIKit`UIApplicationMain + 1211
frame #41: 0x00002a8d TableControllerPractice`main(argc=1, argv=0xbffff2d4) + 141 at main.m:16
frame #42: 0x000029b5 TableControllerPractice`start + 53

1 个答案:

答案 0 :(得分:2)

我在您的init方法中看到了这一点:

[picklerQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        picklersArray = [[NSArray alloc] initWithArray:objects];                
    }
}];

因此,它将在后台线程上启动查询,并在收到查询结果之前继续在主线程上执行。

稍后,当查询结果到达时,它将运行您的块(在主线程上,我推测),此时块设置picklersArray实例变量。

此时,您需要告诉表视图重新加载其数据。试试这个:

[picklerQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        picklersArray = [[NSArray alloc] initWithArray:objects];
        [self.tableView reloadData];
    }
}];