iOS:从自定义方法向表视图添加数据

时间:2013-03-28 09:01:41

标签: ios uitableview

我有一个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添加单元格?

1 个答案:

答案 0 :(得分:0)

如果您只是覆盖此方法:

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

调用[UITableView reloadData]应该可以自行解决。 UITableViewController只询问那里的数据(单元格)(使用“tableView:numberOfRowsInSection:”),并使用第一个提到的方法为每个indexPath请求Cell。