我通过三个视图实现了这一点,通过标准协议和单个视图控制器进行管理。在这个解决方案中,您只关心链接两个表格视图控制器的垂直滚动。水平滚动通过包裹表格视图主体的滚动视图免费提供。我利用以下 UITableViewDelegate 函数来获取锁定的列标题:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
查看结构:
- View - The VCs root view
- Table View - rowHeadersTable - the locked left hand columns
- Scroll View - rowBodyHorizontalScrollView - a container of the table body
- Table View - rowBodyVerticalContentTableView - the table body, as wide as it needs to be beyond the frame of the parent scroll view
视图控制器:
实现的协议:
@interface LockedTableColumnsViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
设置:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.rowHeadersTable setDataSource:self];
[self.rowHeadersTable setDelegate:self];
[self.rowBodyVerticalContentTableView setDataSource:self];
[self.rowBodyVerticalContentTableView setDelegate:self];
}
-(void)viewDidAppear:(BOOL)animated
{
// This ensures the scroll view can only scroll horizontally, and adapts to the size of its member content
[self.rowBodyHorizontalScrollView setContentSize:self.rowBodyVerticalContentTableView.frame.size]; // Important to do this here rather than viewDidLoad, because we want a final reading on self.rowBodyVerticalContentTableView.frame.size
[super viewDidAppear:animated];
}
// Do your Table Data Source however you choose
管理 rowHeadersTable 和 rowBodyVerticalContentTableView 之间的垂直滚动链接:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.rowBodyVerticalContentTableView) {
[self.rowHeadersTable setContentOffset:CGPointMake(self.rowHeadersTable.contentOffset.x, scrollView.contentOffset.y)];
}
else if (scrollView == self.rowHeadersTable) {
[self.rowBodyVerticalContentTableView setContentOffset:CGPointMake(self.rowBodyVerticalContentTableView.contentOffset.x, scrollView.contentOffset.y)];
}
}
NB 这对于长(意味着高)表会很好地执行,因为它使用原型单元格等。但是它没有针对水平密集的行内容进行优化。我的表格正文内容是文本和有限数量的列。如果你没有那么奢侈,你总是可以让你的表格行体成为一个水平流动的集合视图,但是你必须链接你所有的行集合视图偏移量(我猜是通过可见的单元格)。那完全是另一种动物。祝你好运!