UITable view button.tag to get value of row
I am having Tableview which shows comments on facebook post. I am getting comments in Array and comments Id in second array ..I have taken one uibutton in tableview cell. on that button I want to get comment id of particular comment so that I can pass that id to like method..But It is always taking id Of last comment..How can I ID of comment on Button.tag property,, My code is :
- (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] autoreleas开发者_如何学Goe];
}
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];
}
Please help
The problem, which you are facing because you are always using the last like
button.
You require to make few changes, Append an in the function name like likeComment:
.
[like addTarget:self action:@selector(likeComment:) forControlEvents:UIControlEventTouchUpInside];
And modify your likeComment
method like below.
- (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];
}
精彩评论