2014-04-11 60 views
0

我有一个应用程序,您可以在该帖子中添加评论。我使用[发件人标签]获取索引,但它总是返回相同的帖子。因此,无论发布什么帖子,我都会点击评论按钮,并将其添加到同一个单元格中,而不是单击我单击的单元格。使用标签没有得到正确的索引?

任何帮助是超级赞赏。如果你需要看到更多的东西,请让我知道):这里是我的代码(注意,我已经将我的代码剥离到了我认为会使阅读更容易的部分,因为一些函数有很多代码。

设置在每个单元上的评论按钮:

- (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]; 

} 

准备赛格瑞(我送他们到与文本字段的基本视图控制器和保存按钮):

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); 
    }]; 

} 

只是为了澄清它成功地添加注释到数据库只是post.id是不正确的。

回答

1

你确定你正确设置了按钮标签吗?看起来你应该这样设置

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

    [cell.commentButton addTarget:self 
         action:@selector(commentButtonClick:) 
       forControlEvents:(UIControlEvents)UIControlEventTouchDown]; 
    cell.commentButton.tag = indexPath.row; 

    return cell; 

} 
+0

知道我忘记了一件简单的事情。谢谢。 – jckly