我有一个iPhone应用程序使用OAuth连接到服务器。成功时,它从服务器获取用户。再次,成功后,它会向填充表视图的对象数组添加一个项目。下面是执行此代码:iOS:从自定义方法向表格视图添加数据
- (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?
正如我在问题中指定的,如果我使用'[self.tableView reloadData]'它工作正常。这个想法是使用'[self.tableView beginUpdates]'和'[self.tableView endUpdates]',但如果我在我的代码中使用它,它不起作用... – 2013-03-28 09:16:13
为什么你要这样设计呢?据我所知,这不是设计表格视图的常用方式。 – 2013-03-28 09:21:41
当我使用OAuth验证应用程序时,我基本上添加了一个帐户。添加帐户后,我需要在表格视图中添加表格单元格。如果我马上添加帐户,我可以设计它不同的方式,但我需要动态添加表格单元格。我有另一种设计应用程序的方式来实现我想要做的事情,那么欢迎您提出更好的建议。我是这样设计的,因为我不知道更好...... – 2013-03-28 09:30:45