0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Eray Geveci
  • 1,099
  • 4
  • 17
  • 39
  • possible duplicate of [How to pass UITableView IndexPath to UIButton @selector by parameters in iOS?](http://stackoverflow.com/questions/11936126/how-to-pass-uitableview-indexpath-to-uibutton-selector-by-parameters-in-ios) – TheTiger Nov 15 '13 at 12:33

2 Answers2

1

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.

Itesh
  • 199
  • 8
0

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

amar
  • 4,285
  • 8
  • 40
  • 52
  • can you give an example please ? – Eray Geveci Nov 15 '13 at 11:15
  • [cell.detailButton setTag:indexPath.row]; insted of this line use long int tag= indexPath.row*1000 + indexPath.section, [cell.detailButton setTag:tag]; – amar Nov 15 '13 at 11:16
  • Remember its a workaround and a quick fix it has scalability issue and thanku – amar Nov 15 '13 at 11:24
  • Subclass uibutton add a indexpath property and set it directly like you are setting the tag – amar Nov 15 '13 at 11:27
  • Setting tags is not good way to do so, check this answer http://stackoverflow.com/questions/11936126/how-to-pass-uitableview-indexpath-to-uibutton-selector-by-parameters-in-ios/11936294#11936294 – TheTiger Nov 15 '13 at 12:32