iOS图片浏览器

效果图

PhotoBrowse.gif

核心代码

横向滚动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逆时针旋转90度,其宽度等于容器的高度,高度等于容器的宽度。
2、Cell顺时针旋转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方法只能获取可见的Cell,不可见的将获取为nil。
2、可见的Cell存储在数组visibleCells中。
3、selectRowAtIndexPath: animated:scrollPosition:方法不触发
tableView:willSelectRowAtIndexPath:和tableView:didSelectRowAtIndexPath:
4、Cell的selected状态会被存储下来。

Demo地址

https://github.com/codelyw/iOSDemo

参考

https://www.objccn.io/issue-1-1/

推荐阅读

构建TableView
https://www.mikeash.com/pyblog/friday-qa-2013-02-22-lets-build-uitableview.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容