Eray Geveci
Eray Geveci

Reputation: 1129

How to determine a Button click in a Custom UITableViewCell?

i have a TableView with a Custom Cell and a Section Header. The Header shows me the Country and the custom Cell shows me the City.

which look like this:

USA

  • New York

  • Los Angeles

Germany

  • Berlin

  • Frakfurt

every Cell got a Button. after a press it will navigate and push the city name to the detail view .

The Problem is, that i don't know how to determine the city name after i pressed the button.

I had a solution but it doesn't work anymore:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    StandortCell *cell = (StandortCell *)[tableView dequeueReusableCellWithIdentifier:@"ortCell"];
    [cell.detailButton setTag:indexPath.row];
    cell.ortLabel.text = [managedObject valueForKey:@"stadtname"];
}


- (IBAction)detailButton:(id)sender {

    UIView *contentView = [sender superview];
    UITableViewCell *cell = (UITableViewCell *)[contentView superview];
    NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];

    NSInteger section = cellIndexPath.section;

        UIButton * myButton = sender;
    int row=myButton.tag;

   NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:section];

    StrasseViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"strasse"];
    self.selectedStandort = [self.fetchedResultsController objectAtIndexPath:indexPath];
    detail.currentStandort = self.selectedStandort;
    [self.navigationController pushViewController:detail animated:YES];

}

when i use my old code it will just show the correct street row in the first section. it didn't detect the other sections(Countries);

When i choose the first city in the thirt section. it will display me the first city in the first section.

I hope you can help me.

Upvotes: 0

Views: 745

Answers (2)

amar
amar

Reputation: 4345

You need to send row and section information seperately so make tag as

Tag= row*1000+section.

now from tag get section and row value using row= tag/1000 section =tag%1000

use this value to make correct index path or find some better then 2 mins solution to get that.

Caution its a workaround correct will be to

  1. Subclass UIButton
  2. Add a indexpath property int it
  3. make your class to make button object and insted of tag set indexpath property.

in CustomButton.h

@interface CustomButton :UIButton
@property (nonatomic,assign)NSIndexPath *path;
@end

in CustomButton.m

@implementation CustomButton
@synthesize path;
@end

Use this custom button on your table view cell and instead of tag set path property

Upvotes: 0

Itesh
Itesh

Reputation: 199

use this I hope it will solve your purpose.

Create a button pressed method in your view controller

  • (IBAction)detailButton:(UIButton *)sender {

    CustomUITableViewCellName *cell = (CustomUITableViewCellName *)[[sender superview]superview]; NSIndexPath *cellIndexPath = [YOUR_TABLE_NAME indexPathForCell:cell];

}

For ios 7 add another superview.

Now in this way you will get your IndexPath and you an get the current row by using indexPath.row or indexPath.section.

You can get the title using sender.title property.

You can use this indexPath in your array to the required details.

Upvotes: 1

Related Questions