-1

I want to get the some of the data of what is displayed in a cell when I'm tapping on a button which I placed in the cell. For this I have implemented used the indexPathForSelectedRow method. But when I'm tapping on the cell, the method is returning 0 index, even if I'm not tapping on the 0th index cell. This is the code I used for the button -

- (IBAction)dataSaveLounge:(id)sender {

NSIndexPath *path = [self.loungeTable indexPathForSelectedRow];

NSString *titleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"title"];

NSString *subtitleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"excerpt"];

NSString *idSave = [[loungeData objectAtIndex:path.row] valueForKey:@"id"];


saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];

}

EDIT : Added cellfForRowAtIndex Method

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

static NSString *loungeCellID = @"MyCellLounge";
LoungeTableCell *loungeCell = [tableView dequeueReusableCellWithIdentifier:loungeCellID];

if (loungeCell == nil) {

    loungeCell = [[LoungeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCellLounge"];
}

loungeCell.loungeTitle.text = [[loungeData objectAtIndex:indexPath.row]valueForKey:@"title"];
loungeCell.loungeSubtitle.text = [[loungeData objectAtIndex:indexPath.row]valueForKey:@"excerpt"];

dispatch_async(dispatch_get_main_queue(), ^{

    loungeCell.loungeImgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"default"]];

    [loungeCell.loungeImgView sd_setImageWithURL:[NSURL URLWithString:[[loungeData objectAtIndex:indexPath.row] valueForKey:@"banner_image"]]];
});

return  loungeCell;
}

I'm saving some data in core data on button click, hence the last method call

Akshay Yerneni
  • 103
  • 2
  • 11

5 Answers5

0

Try this if you are clicking button in row SET button tag

cell.button.tag = indexPath.row 
[cell.button addTarget:self action:@selector(dataSaveLounge:) forControlEvents:UIControlEventTouchUpInside];

In your cellForRowAtIndexPath

- (IBAction)dataSaveLounge:(id)sender {
UIButton *pressedButton = (UIButton *)sender;
   NSLog(@"Tag of button pressed:%d",pressedButton.tag);
}
karthikeyan
  • 3,821
  • 3
  • 22
  • 45
  • Why is there a need of creating a button inside -(IBAction)dataSaveLounge:(id)sender ? Shouldn't I use the button outlet I created in the tablecell class ? – Akshay Yerneni Mar 28 '17 at 13:35
0

Set button tag in cellForRowAtIndexPath

for e.g. loungeCell.btn = indexPath.row;// set your button IBOutlet name here

and then access here

- (IBAction)dataSaveLounge:(id)sender {

    UIButton *btn = (UIButton *)sender;


    NSString *titleSave = [[loungeData objectAtIndex:btn.tag] valueForKey:@"title"];

    NSString *subtitleSave = [[loungeData objectAtIndex:btn.tag] valueForKey:@"excerpt"];

    NSString *idSave = [[loungeData objectAtIndex:btn.tag] valueForKey:@"id"];


    saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];

    }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Try below code:

Replace your dataSaveLounge method with below method:

- (IBAction)dataSaveLounge:(id)sender {

  CGPoint center= sender.center; 
  CGPoint viewPoint = [sender.superview convertPoint:center toView:self.loungeTable];
  NSIndexPath *path = [self.loungeTable indexPathForRowAtPoint:viewPoint];


  NSString *titleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"title"];

  NSString *subtitleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"excerpt"];

  NSString *idSave = [[loungeData objectAtIndex:path.row] valueForKey:@"id"];


  saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];

}

OR

- (IBAction)dataSaveLounge:(id)sender {

      CGPoint btnPosition = [sender convertPoint:CGPointZero
                                   toView:self.tableView];
      NSIndexPath *path = [self.loungeTable indexPathForRowAtPoint:btnPosition];



      NSString *titleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"title"];

      NSString *subtitleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"excerpt"];

      NSString *idSave = [[loungeData objectAtIndex:path.row] valueForKey:@"id"];


      saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];

    }
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
0

It is risky to use tags for this purpose in a UITableView. If you add or delete any rows, the tag will not change on existing visible rows, and you'll be pointing to the wrong index.

See this answer for a better solution https://stackoverflow.com/a/43036216/1189470

Community
  • 1
  • 1
dmorrow
  • 5,152
  • 5
  • 20
  • 31
0

You can also get NSIndexPath like this way. if you have multiple section than also this code help you. and make sure your button hierarchy in LoungeTableCell

- (IBAction)dataSaveLounge:(id)sender {
     LoungeTableCell *cell = (LoungeTableCell *) sender.superview.superview;
     NSIndexPath *path =  [self.loungeTable indexPathForCell:cell];
}
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29