0

我有一个 iPhone 应用程序使用 OAuth 连接到服务器。成功后,它会从服务器获取用户。同样,一旦成功,它就会将一个项目添加到填充表格视图的对象数组中。这是执行此操作的代码:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    if (editing) {
        [super setEditing:YES animated:YES];

        self.backButton = self.navigationItem.leftBarButtonItem;

        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(signInWithCatapult)];
        self.navigationItem.leftBarButtonItem = leftButton;
    } else {
        [super setEditing:NO animated:YES];

        self.navigationItem.leftBarButtonItem = self.backButton;
    }
}

- (void)signInWithCatapult
{
    [self signOut];

    GTMOAuth2Authentication *auth = [self catapultAuthenticaiton];
    NSURL *authURL = [NSURL URLWithString:@"https://oauth.lvh.me:3000/oauth/authorize"];
    GTMOAuth2ViewControllerTouch *viewController;
    viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth
                                                                 authorizationURL:authURL
                                                                 keychainItemName:kCatapultKeychainItemName
                                                                         delegate:self
                                                                 finishedSelector:@selector(viewController:finishedWithAuth:error:)];

    [[self navigationController] pushViewController:viewController animated:YES];
}

- (GTMOAuth2Authentication *)catapultAuthenticaiton
{
    NSURL *tokenURL = [NSURL URLWithString:kDoorkeeperTokenURL];
    NSString *redirectURI = @"https://catapultcentral.com/iOSClientCallback";

    GTMOAuth2Authentication *auth;

    auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"Catapult Central"
                                                             tokenURL:tokenURL
                                                          redirectURI:redirectURI
                                                             clientID:kDoorkeeperClientID
                                                         clientSecret:kDoorkeeperClientSecret];

    return auth;
}

- (void)signOut
{

}

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)auth
                 error:(NSError *)error
{
    if (error != nil) {
#if DEBUG
        NSLog(@"ERROR: %@", error);
#endif
    } else {
        NSURL *url = [NSURL URLWithString:@"https://api.lvh.me:3000/api/users/me"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
        [fetcher setAuthorizer:auth];
        [fetcher beginFetchWithDelegate:self didFinishSelector:@selector(currentUserFetcher:finishedWithData:error:)];
    }
}

- (void)currentUserFetcher:(GTMHTTPFetcher *)fetcher
          finishedWithData:(NSData *)data
                     error:(NSError *)error
{
    if (error != nil) {
#if DEBUG
        NSLog(@"ERROR: %@", error);
#endif
    } else {
        NSLog(@"Before: %@", self.accounts);
        [self.tableView beginUpdates];
        [self.accounts addObject:@"Success!!!"];
        [self.tableView endUpdates];
//        [self.tableView reloadData];
        NSLog(@"After %@", self.accounts);
    }
}

currentUserFetcher:finishedWithData:error:我将对象添加到self.accounts可变数组的方法中。现在,如果我使用此代码它不起作用:

[self.tableView beginUpdates];
[self.accounts addObject:@"Success!!!"];
[self.tableView endUpdates];

它在以下行失败并[self.tableView endUpdates];显示以下错误消息:

2013-03-28 08:56:21.040 Catapult for iOS[55012:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1054

在线上endUpdates,XCode 抱怨说Thread 1: breakpoint 1.3. 现在,如果我使用此代码,它可以正常工作:

[self.accounts addObject:@"Success!!!"];
[self.tableView reloadData];

现在我怀疑它失败了,因为我向self.accounts实例变量添加了一个对象,但实际上并没有添加单元格。所以我的问题是:如何从currentUserFetcher:finishedWithData:error:方法中向 tableView 添加一个单元格?

4

1 回答 1

0

If you just override this method:

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

Calling [UITableView reloadData] should just work itself out. The UITableViewController will just ask the amount of data (cells) that are there (using "tableView:numberOfRowsInSection:") and is requesting the Cell for every indexPath using the first mentioned method.

于 2013-03-28T09:06:32.597 回答