2014-04-11 3 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]; 

} 

SEGUE 준비가 (I 텍스트 필드에 기본 뷰 컨트롤러로 전송하고 저장 버튼) :

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

그들은 새로운 뷰 컨트롤러에 보내 클릭 그냥 SEGUE을 수행 이 기능은 다음과 같습니다.

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

관련 문제