Swift-UITableView的編輯與移動

  • 1.UITableViewController繼承自UIViewController脆荷,自帶一個(gè)tableview

  • 2.self.view不是UIView而是UITableView

  • 3.DataSource和Delegate默認(rèn)都是self(UITableViewController)-不需要指定代理了

  • 4.開發(fā)中只需要建立UITableViewController子類窑邦,就會幫我們實(shí)現(xiàn)dataSource和Dalagate協(xié)議中一些相關(guān)的方法,比如tableview編輯(增加,刪除鞭执,移動)项郊,以及多少個(gè)分區(qū),每個(gè)分區(qū)有多少個(gè)cell和返回cell視圖的方法锰镀,當(dāng)你需要的時(shí)候只需要打開相應(yīng)的注釋就可以了

  • tableView編輯的步驟:
    1.讓tableView處于編輯狀態(tài)
    2.設(shè)置那些cell可編輯
    3.設(shè)置編輯樣式(刪除娘侍,插入)
    4.提交編輯操作

  • tableView移動步驟
    1.先讓tableview處于編輯狀態(tài)
    2.設(shè)置哪些cell可以移動
    3.提交移動結(jié)果(只需要更新數(shù)據(jù))

文件圖例.png

AppDelegate.swift代碼如下:

import UIKit
/*
 一:UITableViewController繼承自UIViewController,自帶一個(gè)tableView
 二:self.view不是UIView而是UITableView
 三:DataSource和Delegate默認(rèn)都是
 四:
*/
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = #colorLiteral(red: 0.8931787501, green: 0.9535736618, blue: 1, alpha: 1)
        self.window?.makeKeyAndVisible()

        //1.創(chuàng)建導(dǎo)航視圖控制器的根視圖
        let rootVC = RootTableViewController()
        //2.創(chuàng)建導(dǎo)航視圖控制器泳炉,并為他指定根視圖控制器
        let navigation = UINavigationController(rootViewController: rootVC)
        //3.將導(dǎo)航視圖控制器設(shè)置為window的根視圖控制器
        self.window?.rootViewController = navigation
        return true
    }

RootTableViewController.swift代碼如下:

import UIKit

class RootTableViewController: UITableViewController {

    //重用標(biāo)識
    let identifier = "cell"

    //聯(lián)系人字典屬性  [String:[String]]
    var contactSource = ["W":["王哲磊","王浩","王樂","王晨陽"],"C":["陳楊","陳芮"],"B":["邊文達(dá)","寶音"],"L":["李玲","劉二蛋","劉婧","劉福宏"]]
    //存放排好序的key
    var keysArray:[String]?

    override func viewDidLoad() {
        super.viewDidLoad()

        //取出字典contactSource中的key憾筏,排序后賦值給keyArray
        var keys = self.contactSource.keys
        //排序后賦值給keysArray
        keysArray = keys.sorted()

        //        print(keysArray!)
        //注冊cell
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
        //1.讓tableView處于編輯狀態(tài)
        self.navigationItem.rightBarButtonItem = self.editButtonItem


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        //分組的個(gè)數(shù) = 字典中健值對的個(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ù)組對應(yīng)分區(qū)的key值
        let key = keysArray?[section]

        let group = contactSource[key!]

        return (group?.count)!

        return 0
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)

        //取出字典中key對應(yīng)數(shù)組元素的值,賦值給textLable
        //根據(jù)分區(qū)的下標(biāo)取出對應(yīng)的key值
        let key = keysArray?[indexPath.section]
        //根據(jù)key取出字典中的 value 值
        let group = contactSource[key!]
        //根據(jù)cell的下標(biāo)取出數(shù)組中對應(yīng)位置的元素
        let name = group?[indexPath.row]

        cell.textLabel?.text = name

        // Configure the cell...

        return cell
    }
    //添加區(qū)頭標(biāo)題
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return keysArray?[section]
    }
    //添加索引列表
    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return keysArray
    }

    // 2.設(shè)置哪些cell可以編輯
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        //控制前兩個(gè)分區(qū)可以編輯
        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
    }

    // 提交編輯操作(1.修改數(shù)據(jù)源氧腰,2.更新UI)
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        // 取出選中cell所在的分區(qū)對應(yīng)的key值
        let key = keysArray?[indexPath.section]
        //根據(jù)key值取value值數(shù)組
        var group = contactSource[key!]

        if editingStyle == .delete {
            if group?.count == 1 {
                //1.刪除字典中健值對
                contactSource.removeValue(forKey: key!)
                //2.刪除數(shù)組中的元素
                keysArray?.remove(at: indexPath.section)
                //3.更新UI
                let set = NSIndexSet(index: indexPath.section)
                tableView.deleteSections(set as IndexSet, with: .left)
            }else{//一條條刪除cell
                //1.修改數(shù)據(jù)源
                group?.remove(at: indexPath.row)
                //2.刪除之后重新給字典中key對應(yīng)value賦值
                contactSource[key!] = group
                //3.更新UI
                tableView.deleteRows(at: [indexPath], with: .fade)

            }

        } else if editingStyle == .insert {
            //準(zhǔn)備要插入的數(shù)據(jù)
            let name = "逗比"
            //1.修改數(shù)據(jù)源
            group?.append(name)
            //2.重新為contactSource健值對賦值
            contactSource[key!] = group
            //更新UI
            tableView.insertRows(at: [indexPath], with: .right)
            //4.讓tableView重新加載數(shù)據(jù)
            self.tableView.reloadData()
        }
    }
    //設(shè)置刪除按鈕
    override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "刪除"
    }
    // 2.設(shè)置哪些cell可以移動
    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.提交移動結(jié)果
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        //修改數(shù)據(jù)源(先取出,再刪)
        let key = keysArray?[fromIndexPath.section]
        var group = contactSource[key!]
        //先取出數(shù)組中原位置的元素
        let name = group?[fromIndexPath.row]
        //刪除數(shù)組中原來位置元素
        group?.remove(at: fromIndexPath.row)
        //插入數(shù)組中原來位置元素






    }

    /// 限制跨分區(qū)移動
    ///
    /// - Parameters: 參數(shù)
    ///   - tableView: tableView對象,代理的委托人
    ///   - sourceIndexPath: 移動之前cell位置
    ///   - proposedDestinationIndexPath: 移動之后cell的位置
    /// - Returns: cell移動之后最后位置
    override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
        //根據(jù)分區(qū)下標(biāo)判斷分區(qū)是否允許移動古拴,當(dāng)前后的位置在同一個(gè)分區(qū)箩帚,允許移動,返回移動之后的位置斤富,當(dāng)前后位置不在同一個(gè)分區(qū)膏潮,不允許移動,返回移動之前的位置
        if sourceIndexPath.section == proposedDestinationIndexPath.section {
            return proposedDestinationIndexPath
        }else{
            return sourceIndexPath
        }

    }
    /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末满力,一起剝皮案震驚了整個(gè)濱河市焕参,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌油额,老刑警劉巖叠纷,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異潦嘶,居然都是意外死亡涩嚣,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門掂僵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來航厚,“玉大人,你說我怎么就攤上這事锰蓬♂2牵” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵芹扭,是天一觀的道長麻顶。 經(jīng)常有香客問我,道長舱卡,這世上最難降的妖魔是什么辅肾? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮轮锥,結(jié)果婚禮上矫钓,老公的妹妹穿的比我還像新娘。我一直安慰自己舍杜,他們只是感情好新娜,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蝴簇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪匆帚。 梳的紋絲不亂的頭發(fā)上熬词,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼互拾。 笑死歪今,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的颜矿。 我是一名探鬼主播寄猩,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼骑疆!你這毒婦竟也來了田篇?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤箍铭,失蹤者是張志新(化名)和其女友劉穎泊柬,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體诈火,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡兽赁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了冷守。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片刀崖。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖拍摇,靈堂內(nèi)的尸體忽然破棺而出亮钦,到底是詐尸還是另有隱情,我是刑警寧澤授翻,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布或悲,位于F島的核電站,受9級特大地震影響堪唐,放射性物質(zhì)發(fā)生泄漏巡语。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一淮菠、第九天 我趴在偏房一處隱蔽的房頂上張望男公。 院中可真熱鬧,春花似錦合陵、人聲如沸枢赔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽踏拜。三九已至,卻和暖如春低剔,著一層夾襖步出監(jiān)牢的瞬間速梗,已是汗流浹背肮塞。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留姻锁,地道東北人枕赵。 一個(gè)月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像位隶,于是被迫代替她去往敵國和親拷窜。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容