GitHubgit
import UIKit let identifier = "cellIdentifier" class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITableViewDataSourcePrefetching { lazy var tableView: UITableView = { let tempTableView = UITableView(frame: CGRect(x: 0, y: 20, width: self.view.bounds.size.width, height: self.view.bounds.size.height), style: UITableViewStyle.plain) tempTableView.delegate = self // 代理 tempTableView.dataSource = self // 數據源 tempTableView.prefetchDataSource = self // 預處理數據源 return tempTableView }() lazy var rowArray: Array<Int> = { let array = [1,2,3,4,5,6,7,8,9] return array }() override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(self.tableView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } /// MARK: UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { print("numberOfSections:1") return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { print("numberOfRowsInSection_cell的個數:\(self.rowArray.count)") return self.rowArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { print("cellForRowAt_cell:可自定義") var cell = tableView.dequeueReusableCell(withIdentifier: identifier) // 根據 identifier 從緩衝區取出 if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: identifier) } cell?.textLabel?.text = "\(self.rowArray[indexPath.row])" return cell! } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "titleForHeaderInSection" } func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { return "titleForFooterInSection" } func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { print("cell是否能夠編輯") return true } func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { print("cell是否能夠移動") return true } func sectionIndexTitles(for tableView: UITableView) -> [String]? { print("右側索引") return ["a", "b", "c"] } func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { print("根據所點擊的索引定位到相應的Section") if title == "a" { print("點擊 a 定位到第一個 section") return 1 } return 1 } func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { print("編輯的方式") return .delete } func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { print("移動cell後對數據源作相應的處理") } /// MARK: UITableViewDataSourcePrefetching func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) { print("這個協議方法提供一個數組,這個數組提示了按着本次滑動方向,再接下去要碰到哪些indexPaths了,以UITableView爲例,回調過來的這個數組爲當前屏幕邊緣的indexPath的接下去(上或者下)第10個開始算的indexPath,大概一次5個這樣。") } func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) { print("這個協議返回的數組用於在,當咱們快速滑動到某個區域後又馬上按着反方向滑動,那麼本來被預估要出現的幾個indexPath會被取消掉這樣,這個數組就是存儲被取消預估的indexPath。") } /// MARK: UITableViewDelegate func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { print("即將展現cell") } func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { print("即將展現header") } func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { print("即將展現footer") } func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { print("已經結束展現cell") } func tableView(_ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) { print("已經結束展現header") } func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int) { print("已經結束展現footer") } // func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { // //設置cell高度 // } // func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { // //設置header高度 // } // func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { // //設置footer高度 // } // func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { // //預估cell高度 節約時間 // } // func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat { // //預估header高度 節約時間 // } // func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat { // //預估footer高度 節約時間 // } // func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { // //自定義header 視圖 // } // func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { // //自定義footer 視圖 // } // func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { // //擴展的點擊效果 用以自定義點擊效果 // } // func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool { // //點擊cell是否容許高亮 // } // func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { // //已經高亮 // } // func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) { // //解除高亮 // } // func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { // //即將選中cell // } // func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? { // //即將結束選中 // } // func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // //已經選中 // } // func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { // //已經結束選中 // } // func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { // //編輯形式 // } // func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? { // //刪除的title // } // func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { // //編輯動做 // } // func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool { // //編輯內容是否縮進 // } // func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) { // //即將開始編輯 // } // func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) { // //已經結束編輯 // } // func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath { // //移動cell // } // func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int { // //內容縮進 // } // func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool { // //是否展現菜單(複製/粘貼) // } // func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool { // //是否執行菜單中的動做 // } // func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) { // //執行菜單中的動做 // } // func tableView(tableView: UITableView, canFocusRowAtIndexPath indexPath: NSIndexPath) -> Bool { // //返回可否得到焦點 // } // func tableView(tableView: UITableView, shouldUpdateFocusInContext context: UITableViewFocusUpdateContext) -> Bool{ // //返回是否將要更新焦點 // } // func tableView(tableView: UITableView, didUpdateFocusInContext context: UITableViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) { // //已經更新焦點時調用 // } // func indexPathForPreferredFocusedViewInTableView(tableView: UITableView) -> NSIndexPath? { // //返回上一個焦點的indexPath // } }