1

I am trying desperately to make this IBAction just effectively press a cell at a selected row. I have managed to get it to select a row, but I can't work out how to effectively click on this cell! I am only making my first app but I have managed to figure most things out by myself, but just can't seem to find out how to do this, i'm hoping it is a simple solution (or there is a much better way to do it than I have).

Here is the code for my IBAction anyway:

- (IBAction)myButton:(id)sender {

// Specify which cell I wan't to select

NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0];

// Select it

[self.tableView selectRowAtIndexPath:myIP animated:NO scrollPosition:UITableViewScrollPositionTop];  

// Click this cell??

}

Thanks in advance for any help

user2255616
  • 35
  • 1
  • 5

4 Answers4

1

Just tell the delegate that you've selected it

    [self tableView:self.tableView didSelectRowAtIndexPath:myIP];

Assuming that self is your VC that controls the table.

Dmitry Shevchenko
  • 31,814
  • 10
  • 56
  • 62
  • This gives me the following error: no visible @interface for UITableView declares the selector didselectrowatindexpath – user2255616 Apr 24 '13 at 15:14
  • is your view controller subclassed from UITableViewController? If not you need to ensure your tableView property has its datasource and delegate properties set. Also ensure you have the didSelectRowAtIndexPath method implemented. – inks2002 Apr 24 '13 at 15:21
  • My view controller is subclassed from a UIViewController, not a UITableViewController. I am pretty sure I have the datasource and delegate properties set correctly, how do i make sure i have the didSelectRowAtindexPath metho implemented? – user2255616 Apr 24 '13 at 15:27
  • you need to add the delegate and datasource as protocols to your UIViewController...your header should have this at the top line...@interface MainViewController : UIViewController MainViewController is the name of your viewcontroller that holds the tableview. Then just create the method below, - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath. – inks2002 Apr 24 '13 at 15:33
  • I had the delegate + data source in the header line, I tried adding the method as well but it doesn't seem to have changed anything. I also tried using Dmitry's new line of code, which doesn't suggest there are any errors but then crashes when I press the button for the IBAction. :( – user2255616 Apr 24 '13 at 15:45
  • I sort of got it working, but when it selects the row it now effectively does it twice (I have the table set up with a Push Segue to take information from this table on to a detail page). – user2255616 Apr 24 '13 at 16:01
  • I managed to get it working in the end, had to use this instead: [self performSegueWithIdentifier:@"detail" sender:self]; Marked your answer as correct as it did what I asked for, except I was asking for the wrong thing really. Thanks for all your help anyway – user2255616 Apr 24 '13 at 16:11
1

The below stackoverflow answer looks like exactly what you need...

Automatically cell selected UITableView

Community
  • 1
  • 1
inks2002
  • 377
  • 4
  • 13
0

Not a clean way to achieve but as per my understanding, You can add custom UIButton (transparent) on each cell such a way it covers almost complete cell in cellForRowAtIndexPath:, disable row selection. On these button you can use addTarget:action:

Deepesh Gairola
  • 1,252
  • 12
  • 18
0

If your view controller that has the UITableView in it, is not subclassing UITableViewController you need to create an IBOutlet of the UITableView call it myTableView or whatever you'd like, then in your IBAction you can reference it like this:

- (IBAction)myButton:(id)sender {

    // Specify which cell I wan't to select
    NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0];

    // Select it
    [self.myTableView selectRowAtIndexPath:myIP animated:NO scrollPosition:UITableViewScrollPositionTop];  

    // (Per Dmitry's answer)
    [self.myTableView didSelectRowAtIndexPath:myIP];
}
Aaron
  • 7,055
  • 2
  • 38
  • 53