JJStockView
模仿股票表格和課程表师枣,左右滑動時(shí)霞篡,標(biāo)題部分不動,表頭和右邊內(nèi)容一起滑動萧锉,上下滑動時(shí)珊随,表頭不動,所有內(nèi)容一起上下滑動
效果如下
![demo gif](https://github.com/jezzmemo/JJStockView/raw/master/demo.gif)
demo gif
實(shí)現(xiàn)原理
- 頂部不變的頭部用heightForHeaderInSection顯示,用標(biāo)題和內(nèi)容兩部分組成叶洞,內(nèi)容部分是用UIScrollView作為容器
- 內(nèi)容部分鲫凶,用頭部類似的結(jié)構(gòu),cellForRowAtIndexPath實(shí)現(xiàn)Cell,分成左右兩邊部分衩辟,左邊UIView螟炫,右邊用UIScrollView作為容器
- 基于以上的結(jié)構(gòu),在任意一個(gè)UIScrollView滑動的時(shí)候艺晴,頭部的UIScrollView和Cell的UIScrollView一起來滾動,代碼片段如下:
- (void)linkAgeScrollView:(UIScrollView*)sender{
NSArray* visibleCells = [self.stockTableView visibleCells];
for (JJStockViewCell* cell in visibleCells) {
if (cell.rightContentScrollView != sender) {
cell.rightContentScrollView.delegate = nil;//disable send scrollViewDidScroll message
[cell.rightContentScrollView setContentOffset:CGPointMake(sender.contentOffset.x, 0) animated:NO];
cell.rightContentScrollView.delegate = self;//enable send scrollViewDidScroll message
}
}
if (sender != self.headScrollView) {
self.headScrollView.delegate = nil;//disable send scrollViewDidScroll message
[self.headScrollView setContentOffset:CGPointMake(sender.contentOffset.x, 0) animated:NO];
self.headScrollView.delegate = self;//enable send scrollViewDidScroll message
}
_lastScrollX = sender.contentOffset.x;
}
結(jié)構(gòu)
code demo
如何使用
基本上和TableView類似昼钻,首先必須實(shí)現(xiàn)以下DataSource
@protocol StockViewDataSource <NSObject>
@required
//內(nèi)容的行數(shù)
- (NSUInteger)countForStockView:(JJStockView*)stockView;
//內(nèi)容左邊View
- (UIView*)titleCellForStockView:(JJStockView*)stockView atRowPath:(NSUInteger)row;
//內(nèi)容右邊可滑動View
- (UIView*)contentCellForStockView:(JJStockView*)stockView atRowPath:(NSUInteger)row;
@end
Delegate的所有實(shí)現(xiàn)都是可選的:
@protocol StockViewDelegate <NSObject>
@optional
//左上角的固定不動的View
- (UIView*)headRegularTitle:(JJStockView*)stockView;
//可滑動頭部View
- (UIView*)headTitle:(JJStockView*)stockView;
//頭部高度
- (CGFloat)heightForHeadTitle:(JJStockView*)stockView;
//內(nèi)容高度
- (CGFloat)heightForCell:(JJStockView*)stockView atRowPath:(NSUInteger)row;
//點(diǎn)擊每行事件
- (void)didSelect:(JJStockView*)stockView atRowPath:(NSUInteger)row;
@end