展開收合的 TableView Static Cell

Henry Huang
3 min readJun 27, 2018

--

這是看了魏巍老師的教學影片:展開收合的TableView後,想說用Static型態的TableView做出來,因為Static的TableView在使用上是很方便的,像是可以直接將UI Outlet到TableViewController,還有自動對鍵盤的伸縮做處理,不會擋到TextField,所以Static的TableView在有需要輸入多項資料的時候非常好用。

下面為製作方式:

  1. 在storyboard先拉出一個TableViewController,將tableView設定為Static,設定section數量,我是設定3個,第0個section是個人資料那區塊,第1個section是黃色按鈕的變更名稱那塊,第2個section是黃色按鈕E-mail那塊
  2. 設定個section的cell數量分別是7個、5個、4個
  3. 在cell上擺上要顯示的UI
  4. 創一個UITableViewController.swift文件並連接上面的TableViewController
  5. 之後在UITableViewController.swift裡創一個Boolen陣列,用來追蹤要展開或者收合 var isOpens = Array(repeating: false, count: 2 ),因為我第0個section沒有要做展開收合,所以陣列裡只有2個值,預設是false
  6. 在numberOfRowsInSection裡的程式碼:

因為第0個section不會展開收合,所以永遠是7行,第1個section預設是收合所以return1,如果是展開return5行,以此類推

7.在didSelectRowAt裡的程式碼:

因為第0個section沒有展開收合功能,所以在第一行作判別,之後判別是否是按到section內的第一個cell,然後去判斷Boolen陣列內的值,如果true,就改為false,false就改為true,然後tableView.reloadSections,最重要是最下面的tableView.reloadData,一定要寫,不然會怪怪的。

其實也不一定要用按到第一個cell來展開收合,直接放一個buttom應該也可以,因為主要是要介紹展開收合,其他的一些設定如反白顏色那些,就請再自行調整囉。

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Henry Huang
Henry Huang

Written by Henry Huang

大學讀企管/退伍後在業務圈打滾6年後突然有一天決定投入完全沒接觸過的程式設計/目前為一位iOS Developer/熱愛NBA/dafu1231@gmail.com

No responses yet

Write a response