0

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??

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

MendyK, Thanks for your answer.. I guess you are right.. But how should i make the UITableView call UITabelView delegate methods not scrollview delegates methods?
Yep good answer. Note also that the UITableViewDelegate protocol extends the UIScrollViewDelegate protocol...
0

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

7 Comments

Michael, Thanks for the answer. The problem is that the number of tableviews is dynamic. It depends on a provided array of elements from server.
Ah I see that's why you use the tag. Did you add <UITableViewDataSource, UITableViewDelegate> to the interface already?
Or reload the tableView after addingSubview.
I added <UITableViewDataSource, UITableViewDelegate> to my .h file.. I also tried reloading tableview after adding subview but still same behavior..
Nice! Good luck with your app and please mark my answer as useful if it was.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.