效果圖
核心代碼
橫向滾動(dòng)TableView
- (void)p_initContentTableView {
_contentTableView = [[UITableView alloc] init];
_contentTableView.showsVerticalScrollIndicator = NO;
_contentTableView.showsHorizontalScrollIndicator = NO;
_contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_contentTableView.delegate = self;
_contentTableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
[self p_creatDataSource];
[self addSubview:_contentTableView];
[_contentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
make.height.equalTo(self.mas_width);
make.width.equalTo(self.mas_height);
}];
}
cell.transform = CGAffineTransformMakeRotation(M_PI / 2);
1逛揩、TableView逆時(shí)針旋轉(zhuǎn)90度柠傍,其寬度等于容器的高度,高度等于容器的寬度辩稽。
2惧笛、Cell順時(shí)針旋轉(zhuǎn)90度。
分離DataSource
typedef void (^TableViewCellConfigureBlock)(id cell, id item, NSIndexPath *indexPath);
@interface PJPhotoBrowseDataSource : NSObject <UITableViewDataSource>
- (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
- (void)p_creatDataSource {
_dataSource = [[PJPhotoBrowseDataSource alloc] initWithItems:_dataArray cellIdentifier:kcellIdentifier configureCellBlock:^(PJPhotoBrowseCell *cell, NSString *item, NSIndexPath *indexPath) {
cell.photoName = item;
cell.transform = CGAffineTransformMakeRotation(M_PI / 2);
if (indexPath == _initialIndexPath || cell.selected) {
_initialIndexPath = nil;
[cell showScrollBar:YES];
} else {
[cell showScrollBar:NO];
}
}];
_contentTableView.dataSource = _dataSource;
}
其他TableView小技巧
去除多余分割線
_tableView.tableFooterView = [[UIView alloc] init];
小tips
1逞泄、cellForRowAtIndexPath方法只能獲取可見(jiàn)的Cell患整,不可見(jiàn)的將獲取為nil。
2喷众、可見(jiàn)的Cell存儲(chǔ)在數(shù)組visibleCells中各谚。
3、selectRowAtIndexPath: animated:scrollPosition:方法不觸發(fā)
tableView:willSelectRowAtIndexPath:和tableView:didSelectRowAtIndexPath:
4到千、Cell的selected狀態(tài)會(huì)被存儲(chǔ)下來(lái)昌渤。
Demo地址
https://github.com/codelyw/iOSDemo
參考
https://www.objccn.io/issue-1-1/
推薦閱讀
構(gòu)建TableView
https://www.mikeash.com/pyblog/friday-qa-2013-02-22-lets-build-uitableview.html