我用三個視圖實現了這一點,通過標準協議和單個視圖控制器進行管理。在這個解決方案中,你只關心鏈接兩個表視圖控制器的垂直滾動。水平滾動通過包裝表視圖主體的滾動視圖免費提供。我利用下面的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)];
}
}
N.B.由於它使用了原型單元格等,因此這對長時間(意思是高的)表格來說會表現的很好。但是它並不針對水平激烈的行內容進行優化。我的表格內容是文本,列數有限。如果你沒有這種奢侈品,你總是可以使你的表格行身爲水平流動的集合視圖,但是你必須鏈接所有的行集合視圖偏移量(通過我猜測的可見單元格)。那是另外一種動物。祝你好運!
我們已將所有這些功能內置到我們的產品中:http://www.ioscomponents.com/Home/IOSDataGrid – 2014-08-31 23:53:29