溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS實(shí)現(xiàn)無限循環(huán)滾動的TableView實(shí)戰(zhàn)教程

發(fā)布時(shí)間:2020-10-03 23:52:57 來源:腳本之家 閱讀:191 作者:明仔Su 欄目:移動開發(fā)

前言

本文主要給大家介紹了如何實(shí)現(xiàn)一個(gè)可以無限循環(huán)的TableView的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹吧。

先來看看效果:

iOS實(shí)現(xiàn)無限循環(huán)滾動的TableView實(shí)戰(zhàn)教程

思路

條條大路通羅馬,個(gè)人分析下以下思路的可行性:

      1、借鑒無限廣告輪播的思路??尚行圆桓?,主要是列表頭部和尾部的銜接不夠自然,而且快速滑動不夠流暢。

      2、使用TableView+3倍長度dataSource。可行性一般,在使用過程中滑動流暢,但是由于重復(fù)的數(shù)據(jù)源,可能導(dǎo)致在處理事件時(shí)需要特別對數(shù)據(jù)進(jìn)行處理避免重復(fù),另外此方法不能重用,總讓有強(qiáng)迫癥的人感覺不夠優(yōu)雅。。。

      3、使用TableView子類+數(shù)據(jù)源攔截器。可行性較高,在使用過程中滑動流暢,而且在代理方法中并不需要做特殊處理,可封裝重用。

      4、廣大讀者們提供的更優(yōu)秀的思路。

實(shí)現(xiàn)

我們通過創(chuàng)建TableView的子類,在子類中對dataSource進(jìn)行處理。

如果直接將子類自身設(shè)為子類的dataSource,創(chuàng)建另外一個(gè)dataSource作為對外的delegate,將自身不處理的代理消息轉(zhuǎn)發(fā)給對外的delegate,這樣要求自身實(shí)現(xiàn)所有的代理方法,非常蛋疼。

因此,我們創(chuàng)建一個(gè)攔截器,通過攔截器決定將消息發(fā)送到TableView子類內(nèi)部或者是其dataSource,這樣簡潔又比較優(yōu)雅(裝逼)。

注:使用此方法實(shí)現(xiàn)無限循環(huán)的TableView,需要對ObjC的消息轉(zhuǎn)發(fā)有一定理解。

1、創(chuàng)建3倍長度dataSource,并在滑動到頭部或者尾部時(shí)進(jìn)行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、創(chuàng)建一個(gè)攔截器

@interface SUTableViewInterceptor : NSObject

@property (nonatomic, weak) id receiver;
@property (nonatomic, weak) id middleMan;

@end

3、將攔截器設(shè)置為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、在子類中實(shí)現(xiàn)需要加工處理的代理方法

- (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、在攔截器中轉(zhuǎn)發(fā)消息(如果子類實(shí)現(xiàn)了代理方法,則轉(zhuǎn)發(fā)給子類;如果子類沒有實(shí)現(xiàn),則轉(zhuǎn)發(fā)給外部的代理)

@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基本完成,整理一下思路,不難理解我們是通過攔截器將代理消息轉(zhuǎn)發(fā)到子類內(nèi)部,子類內(nèi)部則通過外部代理提供的dataSource來拷貝成3份,來組成一個(gè)3倍于普通長度的TableView,并在其滑動時(shí)進(jìn)行處理,形成可以無限循環(huán)滾動的效果。

這樣,在外部看起來,使用這個(gè)TableView和普通TableView沒有什么不同,但是多了一個(gè)可以循環(huán)滾動的“屬性”,當(dāng)然,你也可以將其封裝成可設(shè)置的屬性,方便切換普通模式和循環(huán)滾動模式。

下面,用這個(gè)TableView的子類來試著創(chuàng)建一個(gè)可以循環(huán)滾動的列表看看:

- (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 個(gè)主播頻道", indexPath.row + 1];
 return cell;
}

怎么樣,強(qiáng)迫癥是不是舒緩了,是不是輕松多了~~~

Demo

GitHub地址:SUTableView

本地下載:http://xiazai.jb51.net/201705/yuanma/SUTableView(jb51.net).rar

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI