1

I have a PrototypeCell with two buttons in it each button will segue to an other detailed view of the displayed item. When I touch a button in the cell, I set a NSNumber to the selected buttons index (1 or 2) and set the cell selected. The cell then changes its color to grey (highlighted), but the UITableViews delegate method didSelectRowAtIndexPath does not get called.

How do I do it the right way? Cant figure it out.

Here's my code so far:

Inside the UITableViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    TwoButtonPrototypeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell setupButtons];
    NSArray *array1 = [entries objectAtIndex:indexPath.row * 2];
    NSArray *array2 = [entries objectAtIndex:indexPath.row * 2 + 1];
    cell.identifier1 = [array1 objectAtIndex:0];
    cell.identifier2 = [array2 objectAtIndex:0];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TwoButtonPrototypeCell *cell = (TwoButtonPrototypeCell *)[tableView cellForRowAtIndexPath:indexPath];
    identifier = cell.touchedButtonIdentifier;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self performSegueWithIdentifier:@"showDetails" sender:self];
}

Inside the TwoButtonPrototypeCell

- (void)setupButtons {
    [button1 addTarget:self action:@selector(touchedButton1) forControlEvents:UIControlEventTouchUpInside];
    [button2 addTarget:self action:@selector(touchedButton2) forControlEvents:UIControlEventTouchUpInside];
}

- (void)touchedButton1 {
    touchedButtonIdentifier = identifier1;
    [self setSelected:YES animated:NO];
}

- (void)touchedButton2 {
    touchedButtonIdentifier = identifier2;
    [self setSelected:YES animated:NO];
}
Larme
  • 24,190
  • 6
  • 51
  • 81
daydr3amer
  • 326
  • 2
  • 19
  • possible duplicate of [Select tableview row programmatically](http://stackoverflow.com/questions/2035061/select-tableview-row-programmatically) – bbarnhart Feb 24 '14 at 17:35
  • Nope, sice I want to select the TableViews row from the cell and not from the TableViewcontroller. I cant acces the tabeView from the cell. – daydr3amer Feb 24 '14 at 17:37
  • Maybe the title was not clear enough, I fixed this. – daydr3amer Feb 24 '14 at 17:39

1 Answers1

1

Implement a protocol inside your cell, then in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath set the delegate

cell.delegate = self;

call the cells delegate method when the button is pressed, for example you have implemented this protocol method inside your tableViewController

    - (void)cellButtonWasPressed:(TwoButtonPrototypeCell*)cell {
        [self tableView:self.tableView didSelectRowAtIndexPath:[self.tableView indexPathForCell:cell]];    

}

so basically inside your cell you do this:

- (void)touchedButton1 {
    touchedButtonIdentifier = identifier1;
    [self.delegate cellButtonWasPressed:self];
}
Basheer_CAD
  • 4,908
  • 24
  • 36