MovieViewController.Swift

import UIKit

class MovieViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {


    //表格属性

    vartable:UITableView=UITableView(frame:CGRect(x:0,y:0,width:scrWidth,height:scrHeight), style:UITableViewStyle.grouped)


    //给表格加载数据的字典

    vartableData = ["武侠":[

        ["name":"《精武英雄》","author":"李连杰"],["name":"《猛龙过江》","author":"李小龙"],["name":"《我是谁》","author":"成龙"]

                    ],


                     "爱情":[

                        ["name":"《小时代》","author":"杨幂"],["name":"《后来的我们》","author":"周冬雨"]

                    ],


                     "科幻":[

                        ["name":"《阿凡达》","author":"周游"],["name":"《机械公敌》","author":"威尔史密斯"],["name":"《钢铁侠》","author":"小罗伯特唐尼"],["name":"《精武英雄》","author":"李连杰"]

                    ],

                    ]


    overridefuncviewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.


        //设置表格的数据和代理

        self.table.dataSource=self;

        self.table.delegate=self;

        self.view.addSubview(self.table)



    }



    // MARK: - -------------UITableViewDataSource--------------

    //返回分区书

    funcnumberOfSections(in tableView:UITableView) ->Int{

        returnself.tableData.count

    }


    //根据分区下标返回每个分区中有多少行

    functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{

        //得到表格字典的key的数值

        letkeyArr =self.tableData.keys

        //通过分区下标,得到该分区对应的key

        letkey = keyArr[keyArr.index(keyArr.startIndex, offsetBy:section)]

        //通过key得到对应的value,这个value是个数组

        letsectionArr =self.tableData[key]

        return(sectionArr?.count)!


    }

    //单元格赋值方法

    functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{

        letidentifier ="cell"

        varcell = tableView.dequeueReusableCell(withIdentifier: identifier)

        ifcell ==nil{

            cell =UITableViewCell(style: .value1, reuseIdentifier: identifier)

        }


        //得到所有key的数组

        letkeyArr =self.tableData.keys

        //通过分区下标得到分区对应的key

        letkey = keyArr[keyArr.index(keyArr.startIndex, offsetBy: indexPath.section)]

        //根据key得到分区的数组

        letsectionArr =self.tableData[key]

        //根据行的下标得到该行对应的字典

        letrowDic = sectionArr![indexPath.row]

        //给cell赋值

        cell?.textLabel?.text= rowDic["name"]

        cell?.detailTextLabel?.text= rowDic["author"]





        returncell!

    }

    //设置分区标题

    functableView(_tableView:UITableView, titleForHeaderInSection section:Int) ->String? {

        //获取所有key的数组

        letkeyArr =self.tableData.keys

        //根据分区下标获取对应的key

        letkey = keyArr[keyArr.index(keyArr.startIndex, offsetBy: section)]


        returnkey

    }

    // MARK: --------------------------UITableViewDelegate---------------------------

    //点击单元格触发

    functableView(_tableView:UITableView, didSelectRowAt indexPath:IndexPath) {

        //字典所有key数组

        letkeyArr =self.tableData.keys

        //选中的单元格所在的分区的key

        letkey = keyArr[keyArr.index(keyArr.startIndex, offsetBy: indexPath.section)]

        //得到分区对应的数组

        letsectionArr =self.tableData[key]

        //得到该选中的单元格对应的字典

        letmovieDic = sectionArr![indexPath.row]

        //将名字和演员拼接成字符串

        letmessage ="\(movieDic["name"]!)-\(movieDic["author"]!)"

        //定义alertController对象

        letalertVC =UIAlertController(title:nil, message: message, preferredStyle: .alert)

        //添加控制按钮

        alertVC.addAction(UIAlertAction(title:"确定", style: .default, handler:nil))

        //弹出提示视图

        self.present(alertVC, animated:true, completion:nil)

    }


    //设置所有单元格都可以编辑

    functableView(_tableView:UITableView, canEditRowAt indexPath:IndexPath) ->Bool{

        return true

    }


    //编辑触发的回调

    functableView(_tableView:UITableView, commit editingStyle:UITableViewCellEditingStyle, forRowAt indexPath:IndexPath) {

        //如果是删除操作

        ifeditingStyle == .delete{

            //得到分区对应的key

            letkey =self.tableData.keys[self.tableData.keys.index(self.tableData.keys.startIndex, offsetBy: indexPath.section)]

            varsectionArr =self.tableData[key]

            sectionArr?.remove(at: indexPath.row)


            self.tableData.updateValue(sectionArr!, forKey: key)

            self.table.reloadData()

        }

    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容