IOS詳解TableView——選項抽屜(天貓商品列表)



在之前的有篇文章講述了利用HeaderView來寫類似QQ好友列表的表視圖。

這里寫的天貓抽屜其實也可以用該方法實現,具體到細節每個人也有所不同。這里采用的是點擊cell對cell進行運動處理以展開“抽屜”。

最后完成的效果大概是這個樣子。



主要的環節:

點擊將可視的Cell動畫彈開。

其他的Cell覆蓋一層半透明視圖,將視線焦點集中在彈出來的商品細分類別中。

再次點擊選中的或其他Cell,動畫恢復到點擊之前所在的位置。

商品細分類別屬于之前寫過的九宮格實現。這里就不貼代碼了。之前的文章:點擊打開鏈接


這里的素材都來自之前版本天貓的IPA。

加載數據


?

- (void)loadData
{NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];NSArray *array = [NSArray arrayWithContentsOfFile:path];NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:array.count];[array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {ProductType *proType = [[ProductType alloc] init];proType.name = dict[@"name"];proType.imageName = dict[@"imageName"];proType.subProductList = dict[@"subClass"];[arrayM addObject:proType];}];self.typeList = arrayM;
}


?

一個ProductType數據模型,記錄名稱,圖片名稱等。


單元格數據源方法

?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{TypeCell *cell = [tableView dequeueReusableCellWithIdentifier:RTypeCellIdentifier];[cell bindProductKind:_typeList[indexPath.row]];return cell;
}


將數據模型的信息綁定到自定義類中進行處理,這個類在加載視圖之后由tableview進行了注冊。

?


下面看看自定義單元格中的代碼

初始化

?

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];if (self) {self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tmall_bg_main"]];//設置clear可以看到背景,否則會出現一個矩形框self.textLabel.backgroundColor = [UIColor clearColor];self.detailTextLabel.backgroundColor = [UIColor clearColor];self.selectionStyle = UITableViewCellSelectionStyleNone;//coverView 用于遮蓋單元格,在點擊的時候可以改變其alpha值來顯示遮蓋效果_coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RScreenWidth, RTypeCellHeight)];_coverView.backgroundColor = [UIColor whiteColor];_coverView.alpha = 0.0;[self addSubview:_coverView];}return self;
}

?


綁定數據

?

- (void)bindProductKind:(ProductType *)productType
{self.imageView.image = [UIImage imageNamed:productType.imageName];self.textLabel.text = productType.name;NSArray *array = productType.subProductList;NSMutableString *detail = [NSMutableString string];[array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {NSString *string;if (idx < 2){string = dict[@"name"];[detail appendFormat:@"%@/", string];}else if (idx == 2){string = dict[@"name"];[detail appendFormat:@"%@", string];}else{*stop = YES;}}];self.detailTextLabel.text = detail;
}


遍歷array然后進行判斷,對string進行拼接然后顯示到細節label上。

?


然后是對點擊單元格事件的響應處理,處理過程會稍微復雜一點


?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{if (!_animationCells){_animationCells = [NSMutableArray array];}if (!_open){[self openTableView:tableView withSelectIndexPath:indexPath];}else{[self closeTableView:tableView withSelectIndexPath:indexPath];}
}


_animationCells用于之后記錄運動的單元格,以便進行恢復。

?


?

- (CGFloat)offsetBottomYInTableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath
{UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];CGFloat screenHeight = RScreenHeight - RNaviBarHeight;CGFloat cellHeight = RTypeCellHeight;CGFloat frameY = cell.frame.origin.y;CGFloat offY = self.tableView.contentOffset.y;CGFloat bottomY = screenHeight - (frameY - offY) - cellHeight;return bottomY;
}


一個私有方法,為了方便之后獲取偏移的高度,這個高度記錄點擊的單元格的高度到屏幕底部的距離。以便進行判斷。

?


比如我們假設彈出的抽屜視圖高度為200,那么如果點擊的單元格到底部的距離超過200,則點擊的單元格以及以上的不用向上偏移,只要將下面的單元格向下移動即可。

但是如果距離小于200,則所有單元格都要進行響應的移動才能給抽屜視圖騰出空間。


按照思路進行開閉操作

?

- (void)openTableView:(UITableView *)tableView withSelectIndexPath:(NSIndexPath *)indexPath
{/******獲取可見的IndexPath******/NSArray *paths = [tableView indexPathsForVisibleRows];CGFloat bottomY = [self offsetBottomYInTableView:tableView withIndexPath:indexPath];if (bottomY >= RFolderViewHeight){_down = RFolderViewHeight;[paths enumerateObjectsUsingBlock:^(NSIndexPath *path, NSUInteger idx, BOOL *stop) {TypeCell *moveCell = (TypeCell *)[tableView cellForRowAtIndexPath:path];if (path.row > indexPath.row){[self animateCell:moveCell WithDirection:RMoveDown distance:_down andStatus:YES];[_animationCells addObject:moveCell];}if (path.row != indexPath.row){//遮蓋視圖改變透明度 讓其他單元格變暗moveCell.coverView.alpha = RCoverAlpha;}}];}else{_up = RFolderViewHeight - bottomY;_down = bottomY;[paths enumerateObjectsUsingBlock:^(NSIndexPath *path, NSUInteger idx, BOOL *stop) {TypeCell *moveCell = (TypeCell *)[tableView cellForRowAtIndexPath:path];if (path.row != indexPath.row){moveCell.coverView.alpha = RCoverAlpha;}if (path.row <= indexPath.row){[self animateCell:moveCell WithDirection:RMoveUp distance:_up andStatus:YES];}else{[self animateCell:moveCell WithDirection:RMoveDown distance:_down andStatus:YES];}[_animationCells addObject:moveCell];}];}//禁止滾動表格視圖tableView.scrollEnabled = NO;
}


主要對可視的單元格進行了判斷移動,

?

其中[self animateCell:moveCell WithDirection:RMoveDown distance:_down andStatus:YES];是一個私有的重構后的方法。

不過一般情況下,動畫的方法盡量在所有需求完成后再進行重構,因為畢竟不同的情況可能處理會很不同(動畫方式,動畫后的處理),放到一個方法后之后可能會發生需要再改回去。

看下這個方法

?

- (void)animateCell:(TypeCell *)cell WithDirection:(RMoveDirection)direction distance:(CGFloat)dis andStatus:(BOOL)status
{CGRect newFrame = cell.frame;cell.direction = direction;switch (direction){case RMoveUp:newFrame.origin.y -= dis;break;case RMoveDown:newFrame.origin.y += dis;break;default:NSAssert(NO, @"無法識別的方向");break;}[UIView animateWithDuration:RCellMoveDurationanimations:^{cell.frame = newFrame;} completion:^(BOOL finished) {_open = status;}];
}


傳入參數為單元格,動畫方向,運動的距離以及一個判斷是否打開的標識位。

?


最后看下閉合操作

?

- (void)closeTableView:(UITableView *)tableView withSelectIndexPath:(NSIndexPath *)indexPath
{[_animationCells enumerateObjectsUsingBlock:^(TypeCell *moveCell, NSUInteger idx, BOOL *stop) {if (moveCell.direction == RMoveUp){[self animateCell:moveCell WithDirection:RMoveDown distance:_up andStatus:NO];}else{[self animateCell:moveCell WithDirection:RMoveUp distance:_down andStatus:NO];}}];NSArray *paths = [tableView indexPathsForVisibleRows];for (NSIndexPath *path in paths){TypeCell *typeCell = (TypeCell *)[tableView cellForRowAtIndexPath:path];typeCell.coverView.alpha = 0;}_up = 0;   //對一系列成員進行處理。_down = 0;tableView.scrollEnabled = YES;[_animationCells removeAllObjects];
}


?


Demo源碼:點擊打開鏈接


不過這個素材來自于之前天貓客戶端的版本,現在的天貓客戶端對商品列表進行了改變。也是彈出,不過彈出的列表內容更多,占據了整個屏幕。



最近一直在寫TableView的博客,常用的大部分都包含到了。

傳送門:

IOS詳解TableView——性能優化及手工繪制UITableViewCell

IOS詳解TableView —— QQ好友列表的實現

IOS詳解TableView——對話聊天布局的實現

IOS詳解TableView——實現九宮格效果

IOS詳解TableView——靜態表格使用以及控制器間通訊



以上就是本篇博客全部內容,歡迎指正和交流。轉載注明出處~


?

轉載于:https://www.cnblogs.com/pangblog/p/3341683.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/376379.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376379.shtml
英文地址,請注明出處:http://en.pswp.cn/news/376379.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Unicode與JavaScript詳解 [很好的文章轉]

上個月&#xff0c;我做了一次分享&#xff0c;詳細介紹了Unicode字符集&#xff0c;以及JavaScript語言對它的支持。下面就是這次分享的講稿。 一、Unicode是什么&#xff1f; Unicode源于一個很簡單的想法&#xff1a;將全世界所有的字符包含在一個集合里&#xff0c;計算機只…

編輯器使用說明

歡迎使用Markdown編輯器寫博客 本Markdown編輯器使用StackEdit修改而來&#xff0c;用它寫博客&#xff0c;將會帶來全新的體驗哦&#xff1a; Markdown和擴展Markdown簡潔的語法代碼塊高亮圖片鏈接和圖片上傳LaTex數學公式UML序列圖和流程圖離線寫博客導入導出Markdown文件豐…

關于產品的一些思考——百度之百度百科

百度百科最近改版了&#xff0c;發現有些地方不符合一般人的行為習慣。 1.新版本排版 首先應該將摘要&#xff0c;簡介&#xff0c;目錄什么的放在左側&#xff0c;而不是右側&#xff0c;因為我們都是從左到右&#xff0c;從上到下觀察事物的&#xff0c;而且百科的東西我們不…

Python3.6 IDLE 使用 multiprocessing.Process 不顯示執行函數的打印

要運行的程序&#xff1a; import os from multiprocessing import Process import timedef run_proc(name):print(Child process %s (%s) Running...%(name,os.getpid()))# time.sleep(5)if __name__ __main__:print("Show Start:")print(Parent process %s. % os…

復制控制

復制構造函數、賦值操作符和析構函數總稱為復制控制。編譯器自動實現這些操作&#xff0c;但類也可以定義自己的版本。 實現復制控制操作最困難的部分&#xff0c;往往在于識別何時需要覆蓋默認版本。有一種特別常見的情況需要類定義自己的復制控制成員&#xff1a;類具有指針成…

python Requests登錄GitHub

工具&#xff1a; python 3.6 Fiddler4 所需要的庫&#xff1a; requests BeautifulSoup 首先抓包&#xff0c;觀察登錄時需要什么&#xff1a; 這個authenticity_token的值是訪問/login后可以獲取&#xff0c;值是隨機生成的&#xff0c;所以登錄前要獲取一下。 注…

你必須懂的 T4 模板:深入淺出

示例代碼&#xff1a;示例代碼__你必須懂的T4模板&#xff1a;淺入深出.rar (一)什么是T4模板&#xff1f; T4&#xff0c;即4個T開頭的英文字母組合&#xff1a;Text Template Transformation Toolkit。 T4文本模板&#xff0c;即一種自定義規則的代碼生成器。根據業務模型可生…

stdafx.h是什么用處, stdafx.h、stdafx.cpp的作用

http://blog.csdn.net/songkexin/article/details/1750396 stdafx.h頭文件的作用 Standard Application Fram Extend沒有函數庫&#xff0c;只是定義了一些環境參數&#xff0c;使得編譯出來的程序能在32位的操作系統環境下運行。Windows和MFC的include文件都非常大&#xff0c…

python3 Connection aborted.', RemoteDisconnected('Remote end closed connection without response'

在寫爬蟲的時候遇到了問題&#xff0c;網站是asp.net寫的 requests.exceptions.ConnectionError: (Connection aborted., RemoteDisconnected(Remote end closed connection without response,)) 于是就抓包分析&#xff0c;發現只要加了’Accept-Language’就好了。。。 A…

id和instancetype的區別

id返回不確定類型的對象&#xff08;也就是任意類型的對象&#xff09;&#xff0c;- (id)arrayWithData;返回的就是不確定類型的對象&#xff0c;如果執行數組的方法&#xff0c; [- (id)arrayWithData objectOfIndex:0]編譯時不會報錯&#xff0c;但運行時會報錯&#xff0c;…

windows下Java 用idea連接MySQL數據庫

Java用idea連接數據庫特別簡單。 首先就是下載好MySQL數據庫的驅動程序。 鏈接&#xff1a;https://dev.mysql.com/downloads/connector/j/ 然后就是選下載版本了&#xff1a; 選個zip格式的嘛。。 下載完后就解壓。打開idea&#xff0c;建立個簡單的項目 找到這個: …

7-2

#include<stdio.h> int main(void) {int i;int fib[10]{1,1};for(i2;i<10;i)fib[i]fib[i-1]fib[i-2];for(i0;i<10;i){printf("%6d",fib[i]);if((i1)%50)printf("\n");}return 0; } 轉載于:https://www.cnblogs.com/liruijia199531/p/3357481.h…

歲月悄然前行,沒有停留的痕跡

歲月悄然前行&#xff0c;沒有停留的痕跡。月落烏啼&#xff0c;總是千年的風霜;濤聲依舊&#xff0c;不見當初的夜晚。走過歲月的痕跡&#xff0c;已是物是人非。我們在歲月的軌道上行走&#xff0c;不要給歲月太多的裝飾&#xff0c;不要給歲月太多的言語。給它我們隨著時光追…

160 - 41 defiler.1.exe

環境&#xff1a; Windows xp sp3 工具&#xff1a; Ollydbg stud_PE LoadPE 先分析一下。 這次的程序要求更改了&#xff0c;變成了這個&#xff1a; defilers reversme no.1 -----------------------The task of this little, lame reverseme is to add some code to…

HDU-2112 HDU Today

http://acm.hdu.edu.cn/showproblem.php?pid2112 怎樣把具體的字母的地點轉換為數字的函數為題目的重點。 HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11385 Accepted Submission(s): 2663 P…

AndEngine引擎之SmoothCamera 平滑攝像機

SmoothCamera:就相當于現實世界的攝像機&#xff0c;要想照到一個物體&#xff0c;要么是攝像機移動&#xff0c;要么是物體移動到攝像頭的范圍內&#xff0c;想要放大或縮小一個物體&#xff0c;要么是物體向前或向后移動&#xff0c;要么是攝像頭變焦 這里討論的就是攝像頭的…

160 - 44 defiler.1.exe

環境&#xff1a; Windows xp sp3 工具&#xff1a; 1.ollydbg 2.exeinfope 0x00 查殼 無殼就下一步 0x01 分析 隨便輸入個錯的&#xff0c;出現了不知道哪國的語言。有個6&#xff0c;應該就是name的長度要大于6吧 OD載入&#xff0c;搜字符串。 00421BD7 |. 807D…

時間與日期處理

主要有以下類&#xff1a; NSDate -- 表示一個絕對的時間點NSTimeZone -- 時區信息NSLocale -- 本地化信息NSDateComponents -- 一個封裝了具體年月日、時秒分、周、季度等的類NSCalendar -- 日歷類&#xff0c;它提供了大部分的日期計算接口&#xff0c;并且允許您在NSDate和N…

C++ new/new operator、operator new、placement new初識

簡要釋義 1.operator new是內存分配函數&#xff08;同malloc&#xff09;&#xff0c;C&#xff0b;&#xff0b;在全局作用域(global scope)內提供了3份默認的operator new實現&#xff0c;并且用戶可以重載operator new。 1 void* operator new(std::size_t) throw(std::bad…

160 - 45 Dope2112.2

環境&#xff1a; Windows xp sp3 工具 1.ollydbg 2.exeinfope 0x00 查殼 還是無殼的Delphi程序 0x01 分析 這次繼續OD載入搜字符串&#xff0c;但是沒找到錯誤信息的字符串。 又因為是Delphi程序&#xff0c;所以可以試一下這樣&#xff1a; OD載入后還是搜字符串&…