Search code examples
xcodeuitableviewuiviewcontrollerdidselectrowatindexpath

Xcode didSelectRowAtIndexPath on tableview not called


I have been looking until 6 o'clock this morning, but I can't figure out why the didSelectRowAtIndexPath method is not being called. I am getting quite desparate on this one.

The tableview is shown properly, but I cannot succeed to enable the selection of a cell.

In the header file , I added:

@interface AlertsTable : UIViewController<UITableViewDelegate, UITableViewDataSource, CMPopTipViewDelegate>{
    UITableView *TableView;
}

@property (nonatomic, retain)   UITableView *TableView;

In the implementation file:

@synthesize TableView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


    //Set the title


    //Format Alerts table
    //self.view.backgroundColor = [UIColor redColor];
    CGFloat sideMargin = 10;
    CGFloat topBottomMargin = 44;
    CGFloat originX = sideMargin;
    // compute width based on view size
    CGFloat sizeWidth = (self.view.bounds.size.width - (sideMargin * 2));
    CGFloat originY = topBottomMargin;
    // compute height based on view size
    CGFloat sizeHeight = (self.view.bounds.size.height - (topBottomMargin * 2));
    //self.view.frame = CGRectMake(originX, originY, sizeWidth, sizeHeight);

    self.TableView = [[UITableView alloc] initWithFrame:CGRectMake(originX, originY, sizeWidth, sizeHeight) style:UITableViewStylePlain];


    //Initialize the array.
    AlertsItems = [[NSMutableArray alloc] initWithObjects: @"Alert 1", @"Alert 2", @"Alert 3" , @"Alert 4", @"Alert 5", @"Alert 6", nil];

[self.TableView setDelegate:self];
[self.TableView setDataSource:self];
[self.view addSubview:TableView];
TableView.userInteractionEnabled = YES;
TableView.allowsSelection = YES;
TableView.allowsSelectionDuringEditing = YES;
NSLog(@"delegate:%@ dataSource:%@", self.TableView.delegate, self.TableView.dataSource);

}

The delegate and datasource are both not nil on the check.
Note that the "Alertstable" inherits from a UIViewController, not a UITableViewController.
This is necessary due to the implementation I chose: the tableview is shown on a popupwindow shown on the screen (using another class that I took from the internet).

This is the didSelectRowAtIndexPath method:

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *alertString = [NSString stringWithFormat:@"Clicked on row #%d", [indexPath row]];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertString message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
    [alert show];

}

The methods:

[super touchesBegan:...];  
[super touchesEnded:...];  
[super touchesMoved:...];  

are not implemented.

I added the AlertTable from another ViewController

AlertsTable *AlertTable = [[AlertsTable alloc] WithButton:sender withArray:self.PopTipViews];
[self.view addSubview:AlertTable.view];

I also implemented:

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


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Set up the cell...
    [[cell textLabel] setText:[AlertsItems objectAtIndex:indexPath.row]];
    NSLog (@"%@",[AlertsItems objectAtIndex:indexPath.row]);
    return cell;

}

Do you have any idea what the problem could be?
I'm not very experienced in Xcode and I'm really stuck.

Edit:

I know that it is not custom, but the link to the project can be found here (perhaps I'm overlooking something:

https://www.bilbu.be/BarConnect_v2.zip

The LoginViewController.xib is not properly linked (it is available in the en.lproj subfolder) so you may need to link first (I noticed too late and I had the file uploaded by a friend before - I cannot re-upload it myself unfortunately).
ViewController VC class calls the AlertsTable VC class. There is more in the project that I suppose you can ignore...
Note that the purpose of this project is only to serve as visual interface prototype. It is not meant to be the best optimized application (this is what other developers will do). If this doesn't work, I will use Photoshop to create the interfaces, but I guess that's not gonna be as convincing...


Solution

  • I successfully compiled your project. The problem is that you're going against the MVC pattern. The controller (AlertsTable) is inside of a view (CMPopTipView). I'd suggest you to rethink the hierarchy of the controllers and views. Hope this will help you.