前言

废话少说,今天来看看如何实现一个可以无限循环的TableView
先来看看效果:

KPfsjVj2wyjh1ncm.gif

无限循环TableView预览.gif

思路

条条大路通罗马,个人分析下以下思路的可行性:

1、借鉴无限广告轮播的思路。可行性不高,主要是列表头部和尾部的衔接不够自然,而且快速滑动不够流畅。

2、使用TableView+3倍长度dataSource。可行性一般,在使用过程中滑动流畅,但是由于重复的数据源,可能导致在处理事件时需要特别对数据进行处理避免重复,另外此方法不能重用,总让有强迫症的人感觉不够优雅。。。

3、使用TableView子类+数据源拦截器。可行性较高,在使用过程中滑动流畅,而且在代理方法中并不需要做特殊处理,可封装重用。

4、广大读者们提供的更优秀的思路。

实现

我们通过创建TableView的子类,在子类中对dataSource进行处理。

如果直接将子类自身设为子类的dataSource,创建另外一个dataSource作为对外的delegate,将自身不处理的代理消息转发给对外的delegate,这样要求自身实现所有的代理方法,非常蛋疼。
因此,我们创建一个拦截器,通过拦截器决定将消息发送到TableView子类内部或者是其dataSource,这样简洁又比较优雅(装逼)。
注:使用此方法实现无限循环的TableView,需要对ObjC的消息转发有一定理解。

1、创建3倍长度dataSource,并在滑动到头部或者尾部时进行contentOffset的reset,显示到中间的位置

- (void)layoutSubviews {
    [self resetContentOffsetIfNeeded];
    [super layoutSubviews];
}
- (void)resetContentOffsetIfNeeded {
    CGPoint contentOffset  = self.contentOffset;
    //头部
    if (contentOffset.y < 0.0) {
        contentOffset.y = self.contentSize.height / 3.0;
    }
    //尾部
    else if (contentOffset.y >= (self.contentSize.height - self.bounds.size.height)) {
        contentOffset.y = self.contentSize.height / 3.0 - self.bounds.size.height;
    }
    [self setContentOffset: contentOffset];
}

2、创建一个拦截器

@interface SUTableViewInterceptor : NSObject
@property (nonatomic, weak) id receiver;
@property (nonatomic, weak) id middleMan;
@end

3、将拦截器设置为TableView子类的dataSource

- (void)setDataSource:(id<UITableViewDataSource>)dataSource {
    self.dataSourceInterceptor.receiver = dataSource;
    [super setDataSource:(id<UITableViewDataSource>)self.dataSourceInterceptor];
}
- (SUTableViewInterceptor *)dataSourceInterceptor {
    if (!_dataSourceInterceptor) {
        _dataSourceInterceptor = [[SUTableViewInterceptor alloc]init];
        _dataSourceInterceptor.middleMan = self;
    }
    return _dataSourceInterceptor;
}

4、在子类中实现需要加工处理的代理方法

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    self.actualRows = [self.dataSourceInterceptor.receiver tableView:tableView numberOfRowsInSection:section];
    return self.actualRows * 3;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath * actualIndexPath = [NSIndexPath indexPathForRow:indexPath.row % self.actualRows inSection:indexPath.section];
    return [self.dataSourceInterceptor.receiver tableView:tableView cellForRowAtIndexPath:actualIndexPath];
}

5、在拦截器中转发消息(如果子类实现了代理方法,则转发给子类;如果子类没有实现,则转发给外部的代理)

@implementation SUTableViewInterceptor
#pragma mark - forward & response override
- (id)forwardingTargetForSelector:(SEL)aSelector {
    if ([self.middleMan respondsToSelector:aSelector]) return self.middleMan;
    if ([self.receiver respondsToSelector:aSelector]) return self.receiver;
    return [super forwardingTargetForSelector:aSelector];
}
- (BOOL)respondsToSelector:(SEL)aSelector {
    if ([self.middleMan respondsToSelector:aSelector]) return YES;
    if ([self.receiver respondsToSelector:aSelector]) return YES;
    return [super respondsToSelector:aSelector];
}
@end

到此,自定义的TableView基本完成,整理一下思路,不难理解我们是通过拦截器将代理消息转发到子类内部,子类内部则通过外部代理提供的dataSource来拷贝成3份,来组成一个3倍于普通长度的TableView,并在其滑动时进行处理,形成可以无限循环滚动的效果。
这样,在外部看起来,使用这个TableView和普通TableView没有什么不同,但是多了一个可以循环滚动的“属性”,当然,你也可以将其封装成可设置的属性,方便切换普通模式和循环滚动模式。

下面,用这个TableView的子类来试着创建一个可以循环滚动的列表看看:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}
- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[SUTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = 150.0;
        [_tableView registerNib:[UINib nibWithNibName:@"LiveCell" bundle:nil] forCellReuseIdentifier:liveCellID];
    }
    return _tableView;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LiveCell * cell = [self.tableView dequeueReusableCellWithIdentifier:liveCellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.descLabel.text = [NSString stringWithFormat:@"第 %ld 个主播频道", indexPath.row + 1];
    return cell;
}

怎么样,强迫症是不是舒缓了,是不是轻松多了~~~

Demo

GitHub地址:SUTableView

Next

布吉岛布吉岛

你可能感兴趣的内容
0条评论
RA

RAnders00

这家伙太懒了,什么都没留下
Owner