Domanda

I have several UITableViews with ready delegate and dataSource methods. How to add the same second action/handler for these tables using subclassing but without of editing their didSelectRowAtIndexPath methods manually?

If I use UIButtons instead of tables then the solution is:

inside the child class of UIButton:

- (void)defaultInit {

    [self addTarget:[PCFlurryManager manager] action:@selector(...) forControlEvents:UIControlEventTouchUpInside];
}

- (void)dealloc {

    [self removeTarget:[PCFlurryManager manager] action:@selector(...) forControlEvents:UIControlEventTouchUpInside];
    [super dealloc];
}

So I can use addTarget for the same event as much times as I want. But how to realize the similar for a table?

È stato utile?

Soluzione

You can assign Tag to your UITableView(s). And then you can access table via its tag.

Something like..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSLog(@"Tag: %d",tableView.tag);
    switch (tableView.tag) {
        case 1:
            //stuff for table 1
        break;

        case 2:
            //stuff for table 2
        break;

        default:
        break;
    }

}

Altri suggerimenti

didSelectRowAtIndexPath give you a object (UITableView *)tableView that indicate this method is call for which tableview.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
         if(tableView==YourFirstTableviewObject) {
                   //write your code for first
         } 
         else if(tableView==YourSecondTableviewObject) {
                   //write your code for second
         }   
         else if(tableView==YourThirdTableviewObject) {
                   //write your code for Third
         }
         ..........
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top