appDelegate.Swift--------------------
import UIKit
importCoreData
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
? ? varwindow:UIWindow?
? ? funcapplication(_application:UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey:Any]?) ->Bool{
? ? ? ? //我的班級(jí)
? ? ? ? letmyClassVC =MyClassViewController()
? ? ? ? letmyClassNav =UINavigationController.init(rootViewController: myClassVC)
? ? ? ? myClassNav.tabBarItem=UITabBarItem.init(title:"我的班級(jí)", image:UIImage.init(named:"喜歡灰.png"), selectedImage:UIImage.init(named:"喜歡藍(lán).png"))
? ? ? ? myClassVC.navigationItem.title="1609D班"
? ? ? ? //移動(dòng)班級(jí)
? ? ? ? letyiDongVC =BwClassViewController()
? ? ? ? letyiDongNav =UINavigationController.init(rootViewController: yiDongVC)
? ? ? ? yiDongNav.tabBarItem=UITabBarItem.init(title:"移動(dòng)學(xué)院", image:UIImage.init(named:""), selectedImage:UIImage.init(named:""))
? ? ? ? yiDongVC.navigationItem.title="移動(dòng)通信學(xué)院"
? ? ? ? //商品
? ? ? ? letgoosVC =GoodsViewController()
? ? ? ? letgoosNav =UINavigationController.init(rootViewController: goosVC)
? ? ? ? goosNav.tabBarItem=UITabBarItem.init(title:"商品", image:UIImage.init(named:""), selectedImage:UIImage.init(named:""))
? ? ? ? goosVC.navigationItem.title = "商品"
? ? ? ? //標(biāo)簽欄
? ? ? ? lettabbar =UITabBarController()
? ? ? ? tabbar.viewControllers= [myClassNav,yiDongNav,goosNav]
? ? ? ? //設(shè)置標(biāo)簽欄標(biāo)題
? ? ? ? self.window?.rootViewController= tabbar
? ? ? ? return true
? ? }
=====================================
Student.swift-------------------------
import UIKit
importFoundation
classStudent:NSObject{
? ? varname =""
? ? varage =0
? ? varheight =0.0
? ? //提供==運(yùn)算符
? 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數(shù)組初始化
? ? varstuArr:[Student] = []
? ? //遍歷原始數(shù)組中的每一個(gè)字典
? ? 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{
? ? ? ? //字符串復(fù)用標(biāo)識(shí)
? ? ? ? letidentifier ="mycell"
? ? ? ? //從復(fù)用池中取出一塊內(nèi)存
? ? ? ? varcell = tableView.dequeueReusableCell(withIdentifier: identifier)
? ? ? ? //判斷是否為空
? ? ? ? ifcell ==nil{
? ? ? ? ? ? //實(shí)例化內(nèi)存
? ? ? ? ? ? cell =UITableViewCell.init(style:UITableViewCell.CellStyle.subtitle, reuseIdentifier: identifier)
? ? ? ? }
? ? ? ? //取出每一行對(duì)應(yīng)的字典數(shù)據(jù)
? ? ? ? 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對(duì)應(yīng)的學(xué)生
? ? ? ? letstu =self.tableData?[indexPath.row]
? ? ? ? //創(chuàng)建彈出的視圖
? ? ? ? letalertVC =UIAlertController.init(title:"您選中的學(xué)生是:", 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)
? ? }
? ? //給表格賦值的數(shù)組
? ? 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]
? ? ]
? ? //給表格賦值的數(shù)組
? ? 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()
? ? }
?? //設(shè)置單元格可否被編輯
? ? functableView(_tableView:UITableView, canEditRowAt indexPath:IndexPath) ->Bool{
? ? ? ? return true
? ? }
? ? //設(shè)置執(zhí)行可編輯的方法
? ? functableView(_tableView:UITableView, commit editingStyle:UITableViewCell.EditingStyle, forRowAt indexPath:IndexPath) {
? ? ? ? //如果是刪除編輯操作
? ? ? ? ifeditingStyle == .delete{
? ? ? ? ? ? self.tableData?.remove(at: indexPath.row)
? ? ? ? ? ? tableView.reloadData()
? ? ? ? }
? ? }
}