Demo 地址 本文地址
可展開型預覽 TableView,介面全開,自由到不像話
前陣子被 HTML 和 CSS 折磨得不成人形,但在一票朋友的施壓下,還是硬擠出時間造了個新輪子:ZYThumbnailTableView。以下按照小學語文老師教的排版方式認真介紹。
把它理解成 TableView 的「皮膚」:一個體積不大但交互能力很強的心臟,四肢高度解耦、可自由更換。每個 cell 其實就是一段業務的縮略視圖,下文統一稱為 thumbnailView。使用者可以透過上下手勢把它展開,露出更多功能視圖塊;這些視圖塊的創建介面全部開放,帶回的參數基本能滿足日常交互所需。當然,tableViewCell 本身也完全由你決定。
規矩,先上效果圖:

運作方式:tableViewCell 充當縮略內容的容器,初始高度受限於 cellHeight;點擊後依縮略視圖的實際內容重新計算完整高度,裝入另一個容器中完整呈現,並可上下拖拽展開附加的功能視圖。
自由客製:除了功能本身,所有可見視圖都開放介面——tableViewCell、頂部擴展視圖(topView)、底部擴展視圖(bottomView)都由你自己提供。
接入簡單:只要把自己的 cell、topView、bottomView 配置給 ZYThumbnailTableViewController 即可。
Block:
- ConfigureTableViewCellBlock = () -> UITableViewCell? - UpdateTableViewCellBlock = (cell: UITableViewCell, indexPath: NSIndexPath) -> Void - CreateTopExpansionViewBlock = (indexPath: NSIndexPath) -> UIView - CreateBottomExpansionViewBlock = () -> UIView
Define:
NOTIFY_NAME_DISMISS_PREVIEW:通知名,用來讓已展開的 thumbnailView 收起。
MARGIN_KEYBOARD_ADAPTATION:自動處理鍵盤遮擋輸入控件後,鍵盤與輸入框之間的間距(底層由 ZYKeyboardUtil 實作)。
TYPE_EXPANSION_VIEW_TOP:處理展開抖動動畫時,頂部擴展控件的標識。
TYPE_EXPANSION_VIEW_BOTTOM:處理展開抖動動畫時,底部擴展控件的標識。
Property:
對外開放:
tableViewCellHeight
tableViewDataList
tableViewCellReuseId
tableViewBackgroudColor
keyboardAdaptiveView:自訂控件中若有不希望被鍵盤遮住的輸入框,賦值給它即可自動處理。
內部私有:
mainTableView、clickIndexPathRow(記錄被點擊 cell 的行號)、spreadCellHeight(存放展開後的真實高度)、cellDictionary(存放所有存活中的 cell)
thumbnailView(縮略視圖)、thumbnailViewCanPan(控制展開手勢是否生效)
animator(UI 物理引擎控制者)、expandAmplitude(展開時抖動動作的振幅)
keyboardUtil(自動處理鍵盤遮擋的工具物件)
Delegate:
- optional func zyTableViewDidSelectRow(tableView: UITableView, indexPath: NSIndexPath)
對外方法:
dismissPreview():讓 thumbnailView 消失。在拿不到 ZYThumbnailTableViewController 參照的地方(如 TopView、BottomView 內部),可以改用 NOTIFY_NAME_DISMISS_PREVIEW 通知。
reloadMainTableView():重新載入列表。
結合 Demo 說明接入步驟。
建立控制器物件:
zyThumbnailTableVC = ZYThumbnailTableViewController()
配置 cell 的必要參數:高度、重用標識、資料來源等。
zyThumbnailTableVC.tableViewCellReuseId = "DIYTableViewCell" zyThumbnailTableVC.tableViewCellHeight = 100.0 // 高度可在任何時候動態調整 zyThumbnailTableVC.tableViewDataList = dataList zyThumbnailTableVC.tableViewBackgroudColor = UIColor.whiteColor() // 不設也預設白色
接著注入你自己的 cell。除了 createCell,這個 Block 裡也能做額外初始化,但要留意它只在需要新建 cell 時被呼叫,重用 cell 不會觸發。
//-------- insert your diy tableview cell
zyThumbnailTableVC.configureTableViewCellBlock = {
return DIYTableViewCell.createCell()
}配置 cell 的 update 方法。這是每個 cell 必經之路,除更新內容外也能附加其他操作。建議盡量使用控制器內部的 dataList 作為資料來源,並時刻保持它是最新的——介面回調更新資料時,別忘了同步更新 zyThumbnailTableVC.tableViewDataList。
zyThumbnailTableVC.updateTableViewCellBlock = { [weak self](cell: UITableViewCell, indexPath: NSIndexPath) -> Void in
let myCell = cell as? DIYTableViewCell
// Post 是我的資料 model
guard let dataSource = self?.zyThumbnailTableVC.tableViewDataList[indexPath.row] as? Post else {
print("ERROR: illegal tableview dataSource")
return
}
myCell?.updateCell(dataSource)
}配置頂部與底部擴展視圖:
兩個 Block 都提供了 indexPath 參數;只是我的 BottomView 業務暫時不需要辨識對應哪個 cell,所以把參數省略了。
這裡還有一行對 keyboardAdaptiveView 的賦值,因為我的 BottomView 內含 TextField,交給 ZYKeyboardUtil 自動處理鍵盤遮擋即可(注意:賦值給 keyboardAdaptiveView 的和送進 Block 的是同一個物件)。
//-------- insert your diy TopView
zyThumbnailTableVC.createTopExpansionViewBlock = { [weak self](indexPath: NSIndexPath) -> UIView in
// Post 是我的資料 model
let post = self?.zyThumbnailTableVC.tableViewDataList[indexPath.row] as! Post
let topView = TopView.createView(indexPath, post: post)!
topView.delegate = self
return topView
}
let diyBottomView = BottomView.createView()!
//-------- let your inputView component not be covered by keyboard automatically (ZYKeyboardUtil)
zyThumbnailTableVC.keyboardAdaptiveView = diyBottomView.inputTextField
//-------- insert your diy BottomView
zyThumbnailTableVC.createBottomExpansionViewBlock = { _ in
return diyBottomView
}與 ZYKeyboardUtil 協同工作的效果:

到這裡,屬於你自己的 thumbnailTableView 就完成了。展開、收起、基本功能都能跑起來;至於 topView、bottomView 內部的具體交互,就回到你自己的擴展控件和自訂 cell 裡實作,ZYThumbnailTableView 會把 cell 的 indexPath 貫通三者,讓它們能互相溝通。
回頭看 Demo 裡的交互是怎麼靠 indexPath 串起來的:

標記為已讀後,小圓點消失;
標記為喜歡後,對應 cell 旁會出現一顆星星。
我在 createView 時把 Block 帶來的 indexPath 存進 topView,點擊 favorite 按鈕時就能以 indexPath 為憑據,透過代理改寫資料來源中的對應狀態;下次 createView 再依 indexPath 取回資料顯示。若互動會改動與 cell 相關的資料,還可以在代理方法裡呼叫 reloadMainTableView() 讓列表重載。
// TopView ---------------------------------------------
class func createView(indexPath: NSIndexPath, post: Post) -> TopView? {
//-------- do something
view.indexPath = indexPath
return view
}
// touch up inside ---------------------------------------------
@IBAction func clickFavoriteButton(sender: UIButton) {
//-------- do something
delegate.topViewDidClickFavoriteBtn?(self)
}
// 代理方法 ---------------------------------------------
func topViewDidClickFavoriteBtn(topView: TopView) {
let indexPath = topView.indexPath
// Post 是我的資料 model
let post = zyThumbnailTableVC.tableViewDataList[indexPath.row] as! Post
post.favorite = !post.favorite
zyThumbnailTableVC.reloadMainTableView()
}導航列樣式的處理也很直接:Demo 裡乾脆在外面直接改 zyThumbnailTableView 的 navigationItem;或者讓 ZYThumbnailTableViewController 去繼承你封裝過導航列的父類。
func configureZYTableViewNav() {
let titleView = UILabel(frame: CGRectMake(0, 0, 200, 44))
titleView.text = "ZYThumbnailTabelView"
titleView.textAlignment = .Center
titleView.font = UIFont.systemFontOfSize(20.0)
// 503f39
titleView.textColor = UIColor(red: 63/255.0, green: 47/255.0, blue: 41/255.0, alpha: 1.0)
zyThumbnailTableVC.navigationItem.titleView = titleView
}一次接觸這麼多 Block,還是要提醒那句老話:注意循環引用。
附一張層級示意圖:

特別感謝公司 UI 朋友的協助,設計上的強迫症已經快追上我在程式碼上的強迫症了。
pod 'ZYThumbnailTableView', '~> 0.2.1'
Relation:@liuzhiyi1992 on Github
License:ZYThumbnailTableView is released under the MIT license. See LICENSE for details.
輪子不一定適合每個人,但把「造輪子擰螺絲」的過程,以及扳手螺絲刀的更多用法毫無保留地分享出來,這件事本身挺有意思的。交流,快樂!
有任何問題歡迎在 GitHub 提 issues 討論,謝謝。