Swift-表格

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

? ? ? ? }

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、簡(jiǎn)介 <<UITableView(或簡(jiǎn)單地說,表視圖)的一個(gè)實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無邪8閱讀 10,959評(píng)論 3 3
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,297評(píng)論 3 38
  • // 屏幕的寬 let SCREEN_WIDTH = UIScreen.main.bounds.size.widt...
    你又臉紅了i閱讀 264評(píng)論 0 1
  • 前言 最近忙完項(xiàng)目比較閑,想寫一篇博客來分享一些自學(xué)iOS的心得體會(huì),希望對(duì)迷茫的你有所幫助。博主非科班出身,一些...
    GitHubPorter閱讀 1,582評(píng)論 9 5
  • 我知道?但是我不會(huì)説破! 那是你的自尊,你選擇了自己的生活方式?包括謊言。 沒有人?是笨的!只是大家都不説。 狐貍...
    蔡振源閱讀 173評(píng)論 0 0

友情鏈接更多精彩內(nèi)容