iOS - 具有多个 UITableViews 的水平 UIScrollView

Posted

技术标签:

【中文标题】iOS - 具有多个 UITableViews 的水平 UIScrollView【英文标题】:iOS - Horizontal UIScrollView with multiple UITableViews 【发布时间】:2013-09-13 10:41:00 【问题描述】:

UIScrollView 水平滚动,UITableView 在其中垂直滚动,但水平滚动时无法加载不同的数据。

有一个滚动视图,它在屏幕上水平滚动,在其中我添加了多个 tableview 并希望在滑动时在 tableview 上显示不同的不同数据。我已经尝试过了,但没有运气:(

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)];

滚动视图中有 4 个页面,其中包括 4 个表视图,我想在滑动滚动视图时在第一个表上显示 1、2、3 和在第二个表上显示 4、5、6,依此类推...

请帮助我提前谢谢。

【问题讨论】:

【参考方案1】:

我看过你的代码,你可以用tag

    在循环中为tableview设置标签

    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;
    
    

现在在 cellForRowAtIndexPath 方法中使用它 喜欢

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

查看ScrollViewDemo

希望对你有帮助。

【讨论】:

+1,我之前尝试过使用标签,但没有正确使用它,感谢您的帮助:)它完美无缺!

以上是关于iOS - 具有多个 UITableViews 的水平 UIScrollView的主要内容,如果未能解决你的问题,请参考以下文章

为啥 -drawRect 比为 UITableViews 使用 CALayers/UIViews 更快?

一个 UIView 上的多个 UITableViews

实现多个 UITableViews 的最佳方式

Xcode中多个Segues中通过UITableViews传递数据(三个Segues)

如何将 UITableViews 用于多个数据集

如何在滚动视图中进行多个表格视图