【发布时间】:2014-04-16 14:52:17
【问题描述】:
我有一个应用程序,您可以在其中将 cmets 添加到帖子中。我正在使用 [sender tag] 来获取索引,但它总是返回相同的帖子。因此,无论在哪个单元格上单击评论按钮,我都会将其添加到同一个单元格中,而不是我单击的那个单元格中。
非常感谢任何帮助。
这是我的代码(请注意,我已将代码剥离为我认为对阅读更容易的部分,因为某些函数有很多代码。如果您需要查看更多代码,请告诉我):
在每个单元格上设置评论按钮:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell.commentButton addTarget:self action:@selector(commentButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
return cell;
}
评论按钮。只是执行一个转场:
- (void)commentButtonClick:(id)sender {
[self performSegueWithIdentifier:@"addCommentSegue" sender:sender];
}
为 segue 做准备(我将它们发送到带有文本字段和保存按钮的基本视图控制器):
else if ([segue.identifier isEqualToString:@"addCommentSegue"]) {
GFAddCommentViewController *secondDestViewController = [[segue destinationViewController] topViewController];
NSInteger index = [sender tag];
NSDictionary *rootObject = self.posts[index];
NSDictionary *post = rootObject[@"post"];
NSDictionary *group = post[@"group"];
secondDestViewController.postId = [post[@"id"] copy];
secondDestViewController.groupName = [group[@"name"] copy];
secondDestViewController.postBody =[post[@"body"] copy];
}
当他们在新的视图控制器上点击发送时,函数如下:
-(void)addComment:(id)sender {
GFCredentialStore *credentialStore = [[GFCredentialStore alloc] init];
NSString * authToken = [credentialStore authToken];
NSString * addCommentURL = [NSString stringWithFormat:@"%s%s/%@/%s", kBaseURL, kPostURL, self.postId, kCommentURL];
NSString * commentBody = self.commentTextField.text;
NSMutableDictionary *mutableParams = [NSMutableDictionary dictionary];
if (commentBody) {
[mutableParams setObject:commentBody forKey:@"comment[body]"];
}
[SVProgressHUD showWithStatus:@"Adding Comment"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:authToken forHTTPHeaderField:@"auth_token"];
[manager POST:addCommentURL parameters:mutableParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
[SVProgressHUD showSuccessWithStatus:@"Comment Added"];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
只是为了澄清它已成功将 cmets 添加到数据库中,只是 post.id 不正确。
【问题讨论】:
标签: ios objective-c cocoa-touch uitableview