Swift-表格

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()

        }

    }

}

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

推荐阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,663评论 3 3
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,085评论 3 38
  • // 屏幕的宽 let SCREEN_WIDTH = UIScreen.main.bounds.size.widt...
    你又脸红了i阅读 231评论 0 1
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,451评论 9 5
  • 我知道⋯但是我不會説破! 那是你的自尊,你選擇了自己的生活方式⋯包括謊言。 沒有人⋯是笨的!只是大家都不説。 狐狸...
    蔡振源阅读 151评论 0 0