Search code examples
objective-cxcodeuitableviewuitouch

call UITableView methods from UITouch


I have a table with customs cells, which has subclass UITableViewCells and for control touches I use UITouch (code based on this tutorial http://gregprice.co.uk/blog/?p=280) I need inside touchesEnded method get index of cell which was touch.

I try:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

   //some code

   UITableView *parentTable = (UITableView *)self.superview;
   NSIndexPath *index = [NSIndexPath alloc];
   index = [parentTable indexPathForSelectedRow];
   NSLog(@"Call, x= %f, index=%d", xPos, index.row);

}

But always I've got index=0.

Whats I do wrong?


Solution

  • it seems like you are taking a very round about way to select a row in a UITableView. set your UITableView's delegate and data source to the class you are working in, then implement its protocol methods which you can find in the UITableViewDelegate and UITableViewDataSource reference. Once you have done that you can use this method to determine which row was selected in the table

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    }
    

    maybe you already knew this but it sounds like you are new to iOS development and i dont see why you would want to manually do it that way.

    and also as Dave Leverton's answer said, dont do an alloc like that