前言
目前成都創(chuàng)新互聯(lián)公司已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、松溪網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
本文主要給大家介紹了如何實(shí)現(xiàn)一個(gè)可以無(wú)限循環(huán)的TableView的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)一起看看詳細(xì)的介紹吧。
先來(lái)看看效果:
思路
條條大路通羅馬,個(gè)人分析下以下思路的可行性:
1、借鑒無(wú)限廣告輪播的思路。可行性不高,主要是列表頭部和尾部的銜接不夠自然,而且快速滑動(dòng)不夠流暢。
2、使用TableView+3倍長(zhǎng)度dataSource??尚行砸话?,在使用過(guò)程中滑動(dòng)流暢,但是由于重復(fù)的數(shù)據(jù)源,可能導(dǎo)致在處理事件時(shí)需要特別對(duì)數(shù)據(jù)進(jìn)行處理避免重復(fù),另外此方法不能重用,總讓有強(qiáng)迫癥的人感覺不夠優(yōu)雅。。。
3、使用TableView子類+數(shù)據(jù)源攔截器??尚行暂^高,在使用過(guò)程中滑動(dòng)流暢,而且在代理方法中并不需要做特殊處理,可封裝重用。
4、廣大讀者們提供的更優(yōu)秀的思路。
實(shí)現(xiàn)
我們通過(guò)創(chuàng)建TableView的子類,在子類中對(duì)dataSource進(jìn)行處理。
如果直接將子類自身設(shè)為子類的dataSource,創(chuàng)建另外一個(gè)dataSource作為對(duì)外的delegate,將自身不處理的代理消息轉(zhuǎn)發(fā)給對(duì)外的delegate,這樣要求自身實(shí)現(xiàn)所有的代理方法,非常蛋疼。
因此,我們創(chuàng)建一個(gè)攔截器,通過(guò)攔截器決定將消息發(fā)送到TableView子類內(nèi)部或者是其dataSource,這樣簡(jiǎn)潔又比較優(yōu)雅(裝逼)。
注:使用此方法實(shí)現(xiàn)無(wú)限循環(huán)的TableView,需要對(duì)ObjC的消息轉(zhuǎn)發(fā)有一定理解。
1、創(chuàng)建3倍長(zhǎng)度dataSource,并在滑動(dòng)到頭部或者尾部時(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基本完成,整理一下思路,不難理解我們是通過(guò)攔截器將代理消息轉(zhuǎn)發(fā)到子類內(nèi)部,子類內(nèi)部則通過(guò)外部代理提供的dataSource來(lái)拷貝成3份,來(lái)組成一個(gè)3倍于普通長(zhǎng)度的TableView,并在其滑動(dòng)時(shí)進(jìn)行處理,形成可以無(wú)限循環(huán)滾動(dòng)的效果。
這樣,在外部看起來(lái),使用這個(gè)TableView和普通TableView沒有什么不同,但是多了一個(gè)可以循環(huán)滾動(dòng)的“屬性”,當(dāng)然,你也可以將其封裝成可設(shè)置的屬性,方便切換普通模式和循環(huán)滾動(dòng)模式。
下面,用這個(gè)TableView的子類來(lái)試著創(chuàng)建一個(gè)可以循環(huán)滾動(dòng)的列表看看:
- (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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)頁(yè)名稱:iOS實(shí)現(xiàn)無(wú)限循環(huán)滾動(dòng)的TableView實(shí)戰(zhàn)教程
網(wǎng)址分享:http://chinadenli.net/article18/gsgjgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、定制網(wǎng)站、電子商務(wù)、建站公司、響應(yīng)式網(wǎng)站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)