3
votes

UIScrollView scrolls Horizontally and UITableView scrolls vertically inside that but unable to load different data when scroll horizontally.

have one scrollview which scrolls horizontally on screen and inside of that i have added multiple tableview and want to display different different datas on tableview when swipe. i have tried this but with no luck :(

NSArray *arr1 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
NSArray *arr2 = [[NSArray alloc] initWithObjects:@"4",@"5",@"6", nil];
NSArray *arr3 = [[NSArray alloc] initWithObjects:@"7",@"8",@"9", nil];
NSArray *arr4 = [[NSArray alloc] initWithObjects:@"10",@"11",@"12", nil];
arrData = [[NSMutableArray alloc] initWithObjects:arr1,arr2,arr3,arr4, nil];

int width=0;

for (int i = 0; i<[arrData count]; i++) {
    tblData = [[UITableView alloc] initWithFrame:CGRectMake(width, 20, 300, 245) style:UITableViewStylePlain];
    tblData.dataSource = self;
    tblData.delegate = self;
    [tblData setBackgroundColor:[UIColor clearColor]];
    tblData.separatorColor = [UIColor blackColor];
    [scrHorizontal addSubview:tblData];
    width+=300;
}

[self.scrHorizontal setContentSize:CGSizeMake(([arrData count])*300, self.scrHorizontal.frame.size.height)];

here 4 pages inside scrollview which includes 4 tableviews i want to display 1,2,3 on first table and 4,5,6 on second table when swipe scrollview and so on...

please help me thanks in advance.

1

1 Answers

4
votes

I have seen your code , you can do with tag,

  1. set tag to tableview in loop

    for (int i = 0; i<[arrData count]; i++) {
    tblData = [[UITableView alloc] initWithFrame:CGRectMake(width, 20, 300, 245) style:UITableViewStylePlain];
    
    tblData.tag=i;
    tblData.dataSource = self;
    tblData.delegate = self;
    [tblData setBackgroundColor:[UIColor clearColor]];
    tblData.separatorColor = [UIColor blackColor];
    [scrHorizontal addSubview:tblData];
    width+=300;
    }
    

now in cellForRowAtIndexPath method just use it like

NSArray *arr = [arrData objectAtIndex:tableView.tag];
cell.textLabel.text = [arr objectAtIndex:indexPath.row];

check it ScrollViewDemo

Hope it Helps.