0

I have two custom UItableViewCell with Same UIButton , How to determine from Which Cell UIButton is called ?

P.S. I Don’t want To use two different methods.

cellForRowAtIndexPath Method.

if (indexPath.row==1) {
static NSString *cellIdentifier = @“CustomeCell1“;

                            NewsFeedCell1 *cell = (NewsFeedCell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.btnPrivacy.tag = indexPath.row;
[cell.btnPrivacy addTarget:self action:@selector(btnPrivacyClicked:) forControlEvents:UIControlEventTouchUpInside];

}
else
{
static NSString *cellIdentifier = @"CustomeCell2”;

                            NewsFeedCell2 *cell = (NewsFeedCell2 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.btnPrivacy.tag = indexPath.row;
[cell.btnPrivacy addTarget:self action:@selector(btnPrivacyClicked:) forControlEvents:UIControlEventTouchUpInside];

}

Button click Methods.

-(IBAction)btnPrivacyClicked:(UIButton *)sender
{
    NSLog(@"Privacy Clicked at : %ld",(long)sender.tag);

    // Here Determine From Which Cell UiButton Is Clicked.
    NewsFeedCell1 *cell = (NewsFeedCell1 *)[self.HomeTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
Naeem
  • 789
  • 1
  • 10
  • 23
  • See my answers here 1. http://stackoverflow.com/a/22827645/790842 and here 2. http://stackoverflow.com/a/30029818/790842. It will surely help. Cheers – iphonic May 20 '15 at 13:05
  • If you mean which _type_ of cell (class NewsFeedCell1 or NewsFeedCell2), use `isKindOfClass`. –  May 20 '15 at 14:02

2 Answers2

1
// *** Use Following code to detect `Cell` ***
CGPoint buttonPosition = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
[self.tableView cellForRowAtIndexPath:indexPath];
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
0

Set tag for cell.btnPrivacy.tag=indexPath.row;

and in button action you will get button btnPrivacy tag i.e sender.tag which is cell index.

Try with this.

Vinod N
  • 148
  • 4
  • Please Read the question carefully, i have already set the Tag to UiButton – Naeem May 20 '15 at 13:08
  • I think you have added this later, I am not able to saw at time of my comment. – Vinod N May 20 '15 at 13:10
  • Yes i have edited the question , but my question is to determine from which cell UIButton is Clicked ? – Naeem May 20 '15 at 13:12
  • Yes, you can get it from above code,just add if condition to check tag 1 for cell1 and other tag for other cell2. – Vinod N May 20 '15 at 13:14
  • I have to determine row also , so i can;t assign static tag to UIButton – Naeem May 20 '15 at 13:16
  • I think this is the same, setting indexPath.row as tag give you specific row.which is index of row in table. Also please brief your question if possible. – Vinod N May 20 '15 at 13:18
  • I have already specified in my question that i want to get from which cell button was pressed. – Naeem May 21 '15 at 04:14