美文网首页
Swift-表格

Swift-表格

作者: 财奴 | 来源:发表于2019-03-04 09:04 被阅读0次

    appDelegate.Swift--------------------

    import UIKit

    importCoreData

    @UIApplicationMain

    class AppDelegate: UIResponder, UIApplicationDelegate {

        varwindow:UIWindow?

        funcapplication(_application:UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey:Any]?) ->Bool{

            //我的班级

            letmyClassVC =MyClassViewController()

            letmyClassNav =UINavigationController.init(rootViewController: myClassVC)

            myClassNav.tabBarItem=UITabBarItem.init(title:"我的班级", image:UIImage.init(named:"喜欢灰.png"), selectedImage:UIImage.init(named:"喜欢蓝.png"))

            myClassVC.navigationItem.title="1609D班"

            //移动班级

            letyiDongVC =BwClassViewController()

            letyiDongNav =UINavigationController.init(rootViewController: yiDongVC)

            yiDongNav.tabBarItem=UITabBarItem.init(title:"移动学院", image:UIImage.init(named:""), selectedImage:UIImage.init(named:""))

            yiDongVC.navigationItem.title="移动通信学院"

            //商品

            letgoosVC =GoodsViewController()

            letgoosNav =UINavigationController.init(rootViewController: goosVC)

            goosNav.tabBarItem=UITabBarItem.init(title:"商品", image:UIImage.init(named:""), selectedImage:UIImage.init(named:""))

            goosVC.navigationItem.title = "商品"

            //标签栏

            lettabbar =UITabBarController()

            tabbar.viewControllers= [myClassNav,yiDongNav,goosNav]

            //设置标签栏标题

            self.window?.rootViewController= tabbar

            return true

        }

    =====================================

    Student.swift-------------------------

    import UIKit

    importFoundation

    classStudent:NSObject{

        varname =""

        varage =0

        varheight =0.0

        //提供==运算符

      static  func==(one:inoutStudent,other:inoutStudent) ->Bool{

        ifone.name== other.name&& one.age== other.age&& one.height== other.height{

            return true

          }

        return false

        }

      convenience  init(name:String,age:Int,height:Double){

            self.init()

            self.name= name

            self.age= age

            self.height= height

        }

      static  funccreateStudentArray(arr:[[String:Any]]) -> [Student] {

        //stu数组初始化

        varstuArr:[Student] = []

        //遍历原始数组中的每一个字典

        fordicinarr{

            letstu =Student()

            stu.name= dic["name"]as!String

            stu.age= dic["age"]as!Int

            stu.height= dic["height"]as!Double

            stuArr.append(stu)

        }

        //将stuArr返回

        returnstuArr

      }

    }

    ================================

    MyClassViewController.swift----------------------------------

    import UIKit

    class MyClassViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

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

            ifletcount =self.tableData?.count{

                returncount

            }

            return0

        }

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

            //字符串复用标识

            letidentifier ="mycell"

            //从复用池中取出一块内存

            varcell = tableView.dequeueReusableCell(withIdentifier: identifier)

            //判断是否为空

            ifcell ==nil{

                //实例化内存

                cell =UITableViewCell.init(style:UITableViewCell.CellStyle.subtitle, reuseIdentifier: identifier)

            }

            //取出每一行对应的字典数据

            letstu =self.tableData?[indexPath.row]

            //给cell赋值

            cell?.textLabel?.text= stu?.name

            cell?.detailTextLabel?.text="年龄\(stu!.age)"

            returncell!

        }

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

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

            //获取选中的cell对应的学生

            letstu =self.tableData?[indexPath.row]

            //创建弹出的视图

            letalertVC =UIAlertController.init(title:"您选中的学生是:", message: stu?.name, preferredStyle:UIAlertController.Style.alert)

            //控制按钮

            letok =UIAlertAction.init(title:"确定", style:UIAlertAction.Style.cancel, handler:nil)

            alertVC.addAction(ok)

            //显示弹窗

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

        }

        //给表格赋值的数组

        varnames:[[String:Any]] = [

            ["name":"王张军","age":21,"height":180.0],

            ["name":"苏旭光","age":21,"height":175.8],

            ["name":"王佳健","age":18,"height":173.0],

            ["name":"秦淑曼","age":15,"height":167.0],

            ["name":"王诗文","age":19,"height":175.0],

            ["name":"何晨旭","age":21,"height":165.0],

            ["name":"于凯欣","age":19,"height":170.0],

            ["name":"张世一","age":21,"height":175.0]

        ]

        //给表格赋值的数组

        vartableData:[Student]?

        overridefuncviewDidLoad() {

            super.viewDidLoad()

            let table = UITableView.init(frame: self.view.frame, style: UITableView.Style.plain)

            table.delegate=self

            table.dataSource=self

            self.view.addSubview(table)

            self.tableData = Student.createStudentArray(arr: names)

            table.reloadData()

        }

       //设置单元格可否被编辑

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

            return true

        }

        //设置执行可编辑的方法

        functableView(_tableView:UITableView, commit editingStyle:UITableViewCell.EditingStyle, forRowAt indexPath:IndexPath) {

            //如果是删除编辑操作

            ifeditingStyle == .delete{

                self.tableData?.remove(at: indexPath.row)

                tableView.reloadData()

            }

        }

    }

    相关文章

      网友评论

          本文标题:Swift-表格

          本文链接:https://www.haomeiwen.com/subject/ldqbuqtx.html