#149 製作多選 table view — 巴氏量表改良版

上次作業有用巴氏量表來練習頁面間的資料傳遞:

但實際應用上很少是單獨一個量表就做一個 App 的,
需要結合許多不同的量表,此時能夠在一個頁面內進行多選題就非常重要。

首先將我們要用的內容存成一個 swift 檔案,將 question 單獨存成一個 enum,並用 Dynamic Prototypes 的 TableCell 做出多選的頁面。

enum Question: String {
case first = "一、進食"
case second = "二、移位(平躺到坐起、床鋪至輪椅)"
case third = "三、個人衛生(包含盥洗、梳頭髮及刮鬍子)"
case fourth = "四、如廁(包含穿脫衣褲、擦拭和沖水)"
case fifth = "五、洗澡"
case sixth = "六、平地走動"
case seventh = "七、上下樓梯"
case eighth = "八、穿脫衣褲鞋襪"
case ninth = "九、大便控制"
case tenth = "十、小便控制"
}
struct qna{
let question: Question
let answers: [Answers]
struct Answers{
let option: String
let score: Int
}
}

將多選的指令放在 override func viewDidLoad() 裡面:

tableView.allowsMultipleSelection = true

依序將問題、回答及分數各自放入 table cell 中:

已經可以多選了!耶~~

接著設定每個 section 都只能單選:

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let selectIndexPathInSection = tableView.indexPathsForSelectedRows?.first(where: {
$0.section == indexPath.section //回傳有選到值的section
}) {
tableView.deselectRow(at: selectIndexPathInSection, animated: false) //取消選擇同section的其他row
}
return indexPath
}

first(where:)的補充

設定每section勾選的選項加總分數:(這個步驟最難,我想了兩天😅)

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {for i in 0...9{
if indexPath.section == i {
scoreInRows[i] = qnas[i].answers[indexPath.row].score
print(scoreInRows)
}
}
}

設定每個section都要填寫,否則跳出警訊:

override func shouldPerformSegue(withIdentifier identifier:String, sender: Any?) -> Bool {
if tableView.indexPathsForSelectedRows?.count == tableView.numberOfSections //選擇的row數量 = 所有section的數量
{ return true } else {
let alertController = UIAlertController(title: "錯誤!", message: "選項未填寫完成", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "ok", style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
return false
}
}

將各題結果存成一個 array 並傳送結果到下一頁再加總:

因為試過如果在同一頁面加總,會沒辦法判斷是否已經全部回答完成。倒不如先把結果都存起來,到下一頁再計算總分。

@IBSegueAction func showADLResult(_ coder: NSCoder) -> resultViewController? {
let controller = resultViewController(coder: coder)
controller?.scoreArray = scoreInRows
return controller
}

如此一來就完成我們單頁多選的巴氏量表了!

GitHub 在此:

--

--