iOS - Horizontal UIScrollView with multiple UITableViews

若如初见. 提交于 2019-12-23 20:01:14

问题


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:


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.



来源:https://stackoverflow.com/questions/18784325/ios-horizontal-uiscrollview-with-multiple-uitableviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!