简体   繁体   中英

UITableView Programmatically inside UIScrollView not calling delegate methods

I am trying to load several UITableViews in UIScrollView using the following code

int x = 0;
for (int i = 0; i < 14; i++) {

        UITableView *testTableView = [[UITableView alloc] initWithFrame:CGRectMake(x, 0, 150, scrollView.bounds.size.height)];
        testTableView.dataSource = self;
        testTableView.delegate = self;
        [testTableView setTag:i];
        [testTableView reloadData];
        [scrollView addSubview:testTableView];
        x+=155;
    }

    [scrollView setContentSize:CGSizeMake(x, scrollView.bounds.size.height)];

The problem with this, the uitableview is not responding to any of the delegate methods.. Any ideas??

Since a tableView is (inherits from) a scrollView, the problem could be that your or any scrollView delegate methods are being called for all the table views, as well as the parent scrollView.

If this is the case, you should perform a check in your delegate method. For example

- (void)scrollViewDidScroll: (UIScrollView *)scrollView
{
    if ([scrollView isEqual:tableView1]){

    }else if ([scrollView isEqual:tableView2]){

    }else if ([scrollView isEqual:parentScrollView]){

    }

}

EDIT:

To answer your comment "how should i make the UITableView call UITabelView delegate methods not scrollview delegates methods"

It doesn't really work like that. When you set testTableView.delegate = self; you are also setting that tableViews scrollView delegate. That means, for example, when the tableView scrolls, the appropriate scrollView delegate methods will be called. As far as I know, the way to do what you are asking would be to check in all your delegate methods

if ([scrollView isEqual:parentScrollView]){
   //do all parentScrollView related things
}

This way, when the delegate method is called by the table views, it will do nothing

See here for more about inheriting protocols.

Bind them to a property:

@property (nonatomic, strong) UITableView *tableView1;
@property (nonatomic, strong) UITableView *tableView2;
@property (nonatomic, strong) UITableView *tableView3;
//etc...

Then in your for loop decide what property is ging to be attached something like this:

if (x == 1)
    self.tableView1 = testTableView;
else if (x == 2)
    self.tableView2 = testTableView;

Set the delegate to self on the properties after binding them ofcourse.

self.tableView1.delegate = self;

Also make sure your ViewController can receive delegate calls from UITableView:

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

--edit--

This image shows the code that worked for me. I pressed a row on tableView with tag 1 twice and got the NSLog twice so delegate method was called:

delegateCalled

App looks like this when pressed tableView with tag 1:

tableView1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM