AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
self.window?.makeKeyAndVisible()
//創(chuàng)建導(dǎo)航視圖控制器的根視圖
let vc = RootTableViewController()
//2.創(chuàng)建導(dǎo)航視圖控制器,并為她制定根視圖控制器
let navigation = UINavigationController(rootViewController: vc)
//3.將導(dǎo)航視圖控制器設(shè)置為window的根視圖控制器
self.window?.rootViewController = navigation
return true
}
RootTableViewController.swift
//不需要手動(dòng)遵循協(xié)議和實(shí)現(xiàn)方法 RootTableViewController
/*
一、UITableViewController繼承自UIViewController,自帶一個(gè)tableview
二、self.view不是UIView而是UITableView
三、DataSource和delegate默認(rèn)都是self(UITableViewController)
四、開發(fā)中只需要建立UITableViewController子類,就會(huì)幫我們實(shí)現(xiàn)datasource和delegate協(xié)議中一些相關(guān)方法,比如tableView編輯(增加/刪除、移動(dòng)),以及多少分區(qū),每個(gè)分區(qū)有多少cell和返回cell視圖的方法,當(dāng)你需要的時(shí)候只需要打開相應(yīng)的注釋即可
/
/
tableView編輯的步驟:
1.讓tableView處于編輯狀態(tài)
2.協(xié)議設(shè)定:
2.1設(shè)置那些cell可以編輯
2.2設(shè)置編輯樣式(插入刪除)
2.3提交編輯操作(1.先修改數(shù)據(jù)源2.更新UI)
/
/
tableView移動(dòng)的步驟
1.讓tableView處于編輯狀態(tài)
2.設(shè)置哪些cell可以移動(dòng)
3.提交移動(dòng)結(jié)果(1.只需要更新數(shù)據(jù)源即可)
*/
class RootTableViewController: UITableViewController {
// 聯(lián)系人字典屬性 var dic:[String:[String]] 字典類型
var contactsource = [
"w":["王哲磊","王浩","王樂","王晨陽"],"c":["陳揚(yáng)","陳芮"],"b":["邊文達(dá)","寶音","白婧"],"l":["劉二蛋","劉婧","劉福宏","李玲"]]
//存放排好序的key值
var keysArray:[String]?
//重用標(biāo)識(shí)
let identifier = "cell"
override func viewDidLoad() {
super.viewDidLoad()
//取出字典contactsource中的key
let keys = self.contactsource.keys
//,排序后賦值給keyArray
keysArray = keys.sorted()
// print(keysArray!)
//注冊(cè)限cell
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
//分組個(gè)數(shù)=字典中鍵值對(duì)的個(gè)數(shù)
//分組個(gè)數(shù)=存放排好序key值數(shù)組元素個(gè)數(shù)
return contactsource.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
//先取出排好序key值數(shù)組中對(duì)應(yīng)分區(qū)的key值
let key = keysArray?[section]
let group = contactsource[key!]
return (group?.count)!
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
//取出字典中key值對(duì)應(yīng)數(shù)組元素的值賦值給textLabel
//根據(jù)分區(qū)的下標(biāo)取出key
let key = keysArray?[indexPath.section]
//根據(jù)key取出字典中的value。數(shù)組
let group = contactsource[key!]
//根據(jù)cell的下標(biāo)取出數(shù)組對(duì)應(yīng)位置的元素
let name = group?[indexPath.row]
cell.textLabel?.text = name
return cell
}
//頭部標(biāo)題
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "\(keysArray![section])"
}
//添加右側(cè)索引列表
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return keysArray
}
/* 2.設(shè)置哪些cell可以編輯*/
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
if indexPath.section < 2{
return true
}
return false
}
//3。設(shè)置編輯樣式(插入/刪除)
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
if indexPath.section == 0{
return .delete
}else if indexPath.section == 1{
return .insert
}
return .none
}
/*4.提交編輯操作*/
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
//先取出選中cell所在的分區(qū)對(duì)應(yīng)的key值
let key = keysArray?[indexPath.section]
//根據(jù)key值取出對(duì)應(yīng)的value值數(shù)組
var group = contactsource[key!]
// print(group)
if editingStyle == .delete {
if group?.count == 1{
//刪除整個(gè)分區(qū)
//刪除字典中的鍵值對(duì)
contactsource.removeValue(forKey: key!)
//根據(jù)key值刪除索引欄 刪除排好序的
keysArray?.remove(at: indexPath.section)
//更新UI
let set = NSIndexSet(index: indexPath.section)
tableView.deleteSections(set as IndexSet, with: .left)
}else{
//一條一條的刪除cell
//1.修改數(shù)據(jù)源
group?.remove(at: indexPath.row)
// print(group)
// print(contactsource[key!])
//刪除之后重新為key對(duì)應(yīng)的value賦值
contactsource[key!] = group
//2.更新UI
tableView.deleteRows(at: [indexPath], with: .fade)
}
} else if editingStyle == .insert {
//準(zhǔn)備要插入的數(shù)據(jù)
let name = "小太陽"
//先修改數(shù)據(jù)源
group?.append(name)
//重新為字典contactsource的鍵值對(duì)賦值
contactsource[key!] = group
//更新UI
tableView.insertRows(at: [indexPath], with: .right)
//4.讓tableView重新加載數(shù)據(jù)
tableView.reloadData()
}
}
//2.設(shè)置哪些cell可以移動(dòng)
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
/*3.提交移動(dòng)的結(jié)果 */
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
//修改數(shù)據(jù)源
//g
let key = keysArray?[fromIndexPath.section]
var group = contactsource[key!]
//先取出數(shù)組中原位置的元素
let name = group?[fromIndexPath.row]
//刪除數(shù)組中原來位置的元素
group?.remove(at: fromIndexPath.row)
//在插入數(shù)組中新的位置
group?.insert(name!, at: to.row)
//從新對(duì)字典中key值對(duì)應(yīng)的value值進(jìn)行賦值
contactsource[key!] = group
}
//限制跨分區(qū)移動(dòng)
/*sourceIndexPath移動(dòng)之前cell的位置
proposedDestinationIndexPath移動(dòng)之后cell的位置
command + option + /
*/
/// 限制cell跨分區(qū)移動(dòng)
///
/// - Parameters:參數(shù)
/// - tableView: tableView對(duì)象 代理的委托人
/// - sourceIndexPath: 移動(dòng)之前cell的位置
/// - proposedDestinationIndexPath: 移動(dòng)之后cell的位置
/// - Returns: 返回值cell移動(dòng)之后最終的位置
override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
//根據(jù)分區(qū)的下標(biāo)判斷是否允許移動(dòng),當(dāng)前后位置在同一個(gè)分區(qū),允許移動(dòng),返回值移動(dòng)之后的位置,當(dāng)前后的位置不在同一個(gè)分區(qū),不允許移動(dòng),返回移動(dòng)之前的位置
if sourceIndexPath.section == proposedDestinationIndexPath.section{
return proposedDestinationIndexPath
}else{
return sourceIndexPath
}
}