繼續(xù)上一部分開始用Swift開發(fā)iOS 10 - 7 定制Table Views項目FoodPin的代碼,添加兩個功能:
- 點擊cell時,產(chǎn)生彈框,彈框中有兩個功能選項 Call 和 Check-in
- 當(dāng)點擊Check-in選項時,為cell加上對號
理解UITableViewDelegate協(xié)議
代理模式在iOS編程中非常常見的。每個代理負(fù)責(zé)特定的角色或任務(wù),維持系統(tǒng)的簡單和干凈。當(dāng)一個對象需要完成特定任務(wù)時,可以依靠另一個對象完成。這在軟件設(shè)計模式中通常叫做"separation of concerns"。
UITableViewController
就應(yīng)用了代理模式。兩個協(xié)議完成不同的任務(wù)。UITableViewDataSource
協(xié)議負(fù)責(zé)提供和管理table的數(shù)據(jù)。UITableViewDelegate
協(xié)議負(fù)責(zé)設(shè)置table的section headings 和 footers,以及操作cell selections和cell recording。
閱讀文檔
怎么知道
UITableViewDelegate
中有那些方法呢?
閱讀Apple的官方iOS開發(fā)文檔(https://developer.apple.com/library/ios/))。作為iOS開發(fā)者,需要經(jīng)常閱讀API 文檔。目前沒有單個的書籍可覆蓋所有iOS SDK。 Xcode中提供方便的查看文檔的方法,在相關(guān)代碼處option+點擊,出現(xiàn)彈框展示文檔簡介,再在彈框上點擊相關(guān)代碼就直接進入詳細(xì)文檔處。
文檔對不同方法進行了分類,比如UITableViewDelegate
的文檔就有上面提到的Managing Selections:
func tableView(UITableView, willSelectRowAt: IndexPath)
func tableView(UITableView, didSelectRowAt: IndexPath)
實現(xiàn)協(xié)議中管理列選項方法
- 在
RestaurantTableViewController
類中實現(xiàn)tableView(_:didSelectRowAt:)
方法:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
// 1
let optionMenu = UIAlertController(title: nil, message: "What do you want
to do?", preferredStyle: .actionSheet)
// 2
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler:
nil)
optionMenu.addAction(cancelAction)
// 3
present(optionMenu, animated: true, completion: nil)
}
- 1 創(chuàng)建了
UIAlertController
。UIAlertController
是從iOS8被引入,用來替代以前的UIAlertView
和UIActionSheet
,向用戶彈出警示信息;preferredStyle
參數(shù)有兩個值:.actionSheet
和.alert
,表示替代的兩種樣式。
- 2 創(chuàng)建一個cancel樣式的
UIAlertAction
。UIAlertController
的警示彈出框一般是由最上面的title+message(都不是非必須的)和一些Action組成,cancel action在最下面。
- 3
present
是UIViewController
中的方法,用于展示。
添加Actions到ALert Controller中
- Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message:
"Sorry, the call feature is not available yet. Please retry later.",
preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler:
nil))
self.present(alertMessage, animated: true, completion: nil)
}
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)",
style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)
callActionHandler
是swift閉包結(jié)構(gòu)的一種寫法,關(guān)于閉包可查看以擼代碼的形式學(xué)習(xí)Swift-7:Closure。這個代碼塊在Call action被點擊是執(zhí)行。
- Check-in action
// Check-in action
let checkInAction = UIAlertAction(title: "Check in", style: .default, handler:
{
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .checkmark
})
optionMenu.addAction(checkInAction)
當(dāng)用戶點擊“Check in”action時,為選中的cell增加一個對號,表明是用戶喜歡的。accessoryType
包括disclosureIndicator
, detailDisclosureButton
, checkmark
和 detailButton
。
當(dāng)cell被選中后,這個cell會一直灰色高亮。去掉:
tableView.deselectRow(at: indexPath, animated: false)
解決Bug
現(xiàn)在應(yīng)用運行后,check-in某一cell時,可能會出現(xiàn)其他cell同時也被check-in。
這個問題是因為cell重復(fù)使用導(dǎo)致的。當(dāng)一個屏幕滾動時,新進的cell就會利用滾出的得cell,以提高效率。解決方法:為每個cell創(chuàng)造一個是否被check-in的標(biāo)志。
- 在
RestaurantTableViewController
中創(chuàng)建一個Boolean
類型的數(shù)組:
var restaurantIsVisited = Array(repeating: false, count: 21)
- check-in后就把對應(yīng)的標(biāo)志修改為true:
let checkInAction = UIAlertAction(title: "Check in", style: .default, handler:
{
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .checkmark
self.restaurantIsVisited[indexPath.row] = true
}
- 在每一次生成cell是也要檢查是否check-in,在
tableView(_:cellForRowAt:)
放的return cell
之前加入:
cell.accessoryType = restaurantIsVisited[indexPath.row] ? .checkmark : .none
練習(xí)
當(dāng)選中也被check-in的cell時,check-in Action的文本變成Undo Check in,點擊后取消其對號。修一下check-in代碼:
let cell = tableView.cellForRow(at: indexPath)
if cell?.accessoryType == .checkmark {
let checkInAction = UIAlertAction(title: "Undo Check in", style: .default, handler:
{
(action:UIAlertAction!) -> Void in
cell?.accessoryType = .none
self.restaurantIsVisited[indexPath.row] = false
})
optionMenu.addAction(checkInAction)
} else {
let checkInAction = UIAlertAction(title: "Check in", style: .default, handler:
{
(action:UIAlertAction!) -> Void in
cell?.accessoryType = .checkmark
self.restaurantIsVisited[indexPath.row] = true
})
optionMenu.addAction(checkInAction)
}
代碼
Beginning-iOS-Programming-with-Swift
說明
此文是學(xué)習(xí)appcode網(wǎng)站出的一本書 《Beginning iOS 10 Programming with Swift》 的一篇記錄
系列文章目錄
- 開始用Swift開發(fā)iOS 10 - 1 前言
- 開始用Swift開發(fā)iOS 10 - 2 Hello World!第一個Swift APP
- 開始用Swift開發(fā)iOS 10 - 3 介紹Auto Layout
- 開始用Swift開發(fā)iOS 10 - 4 用Stack View設(shè)計UI
- [開始用Swift開發(fā)iOS 10 - 5 原型的介紹]
- 開始用Swift開發(fā)iOS 10 - 6 創(chuàng)建簡單的Table Based App
- 開始用Swift開發(fā)iOS 10 - 7 定制Table Views
- 開始用Swift開發(fā)iOS 10 - 8 Table View和UIAlertController的交互