2

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?

2 Answers2

0

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;
    }

}
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
  • it is better than the previous answer but by using this way you still need to edit this method manually – user2412679 May 23 '13 at 12:20
  • @user2412679 what do you mean by **"you still need to edit this method manually"** ? – βhargavḯ May 23 '13 at 12:23
  • As I wrote I have several tables and each of them with its own didSelectRowAtIndexPath. So I need to remove them and copy-paste these instances of this method to another one class. For example when I use button I don't need to change/copy its own method for "touch up inside" event, because I can change its class only. So I can write a new code separately, without doubling and even without reassigning of delegate – user2412679 May 23 '13 at 14:17
  • @user2412679 You can create category over UITableView for this purpose. – βhargavḯ May 24 '13 at 03:44
  • thanks I will vote up you. But I think I have found a better solution – user2412679 May 24 '13 at 07:16
  • Please share your link for solution if you have and if you don't mind – βhargavḯ May 24 '13 at 07:18
  • I think I could create a category with this method and call the original method from it: http://stackoverflow.com/questions/1085479/override-a-method-via-objc-category-and-call-the-default-implementation – user2412679 May 24 '13 at 07:25
0

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
         }
         ..........
    }
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
  • 1)in this answer you edit this method; 2)it causes a lot of code if the objects of these tables are in various classes – user2412679 May 23 '13 at 11:18