UITableview button.tag获取行的值

时间:2011-06-17 07:12:40

标签: iphone uitableview

我正在使用Tableview来显示对facebook帖子的评论。我在Array中获得注释并在第二个数组中注释Id ..我在tableview单元格中占用了一个uibutton。在那个按钮上我想得到特定评论的评论ID,这样我就可以将那个id传递给类似方法..但是它总是记录最后一条评论的ID。我怎样才能在Button.tag属性上发表评论ID,我的代码是:

    - (NSInteger)tableView:(UITableView *)atableView numberOfRowsInSection:(NSInteger)section {

        return [[facebook comments] count];
    }


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

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }

    like = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        like.frame = CGRectMake(60, 70, 40, 20);

        [like setTitle:@"like" forState:UIControlStateNormal];
        [like setTag:indexPath.row];
        [like addTarget:self action:@selector(likeComment) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:like];

        ;


        cell.textLabel.font = [UIFont fontWithName:@"Arial" size:10.0];

        cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0];

        cell.textLabel.text = [[facebook commentId]objectAtIndex:indexPath.row];    

        cell.detailTextLabel.text = [[facebook comments]objectAtIndex:indexPath.row];

        NSLog(@"%@ person like this",[facebook likesCount] );


        NSLog(@"%@ person like this comment",[facebook commentLikes]);



        return cell;
    }

    - (void )likeComment {

        NSString *idStr = [NSString stringWithFormat:@"%@/likes",[[facebook commentId]objectAtIndex:like.tag]];



        [fbGraph doGraphPost:idStr withPostVars:nil];   

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"You like this"
                                                       message:@"" delegate:nil
                                             cancelButtonTitle:@"Ok" otherButtonTitles:nil ];

        [alert show];
        [alert release];




}

请帮忙

1 个答案:

答案 0 :(得分:1)

您遇到的问题是因为您始终使用最后 like 按钮

您需要进行一些更改,请在函数名称中添加likeComment:

    [like addTarget:self action:@selector(likeComment:) forControlEvents:UIControlEventTouchUpInside];

并修改您的likeComment方法,如下所示。

- (void )likeComment:(id) sender {

        UIButton* myLikeButton = (UIButton*)sender;
        NSString *idStr = [NSString stringWithFormat:@"%@/likes",[[facebook commentId]objectAtIndex:myLikeButton.tag]];



        [fbGraph doGraphPost:idStr withPostVars:nil];   

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"You like this"
                                                       message:@"" delegate:nil
                                             cancelButtonTitle:@"Ok" otherButtonTitles:nil ];

        [alert show];
        [alert release];
}