Reputation: 639
I am creating a commenting application that displays a number of comments in a table view from an array. Users can like, dislike or flag comments. I have incorporated this in the application, however, whenever a user clicks like, dislike or flag, the first comment is always actioned.
This is the first couple of lines in my cellForRowAtIndexPath:
NSDictionary *myArray = [commentArray objectAtIndex:indexPath.row];
commentID = [myArray objectForKey:@"ID"];
I've tried to send the the commentID as the tag of the button, but I then realised that it was an ID that included many letters, e.g. 7c3769f28c9547f4b6889201a8c13f1e.
Any help would be appreciated. Thanks
Upvotes: 0
Views: 81
Reputation: 9012
Easiest way around this is to set the tag of the button as the indexPath.row
of the cell when you create it.
Then in your likeButtonPressed:
and other button handler methods you can use the button's tag to get the data from the correct index:
-(void)likeButtonPressed:(id)sender
{
UIButton *button = sender;
NSDictionary *commentData = commentArray[button.tag];
// Do what you want with commentData here...
}
Upvotes: 2
Reputation: 2139
You can use this code:
cellForRowAtIndexPath Code
btnlike.tag = indexPath.row
[btnlike addTarget:self action:@selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
likeButtonPressed Method
-(IBAction)likeButtonPressed:(id)sender{
NSLog(@"Button tag :%d",[sender tag]);
}
Upvotes: 0
Reputation: 17
I Had implemented the same concept in my app like this way, Create a nsobject class,declare and implement buttons in that,the in your class cell for row method write like this way, cell =(customCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell = [[DetailsRoomsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.buttonRoomType1.tag = indexPath.row; [cell.button1 addTarget:self action:@selector(aMethod1:) forControlEvents:UIControlEventTouchDown]; cell.button2.tag = indexPath.row+1; [cell.buttonRoomType2 addTarget:self action:@selector(aMethod2:) forControlEvents:UIControlEventTouchDown];
this will help you.
Upvotes: 0