SIWFT 學(xué)習(xí)之 TodoList

實(shí)現(xiàn)效果:

沒(méi)有數(shù)據(jù)時(shí),底部區(qū)域顯示空?qǐng)D片,輸入數(shù)據(jù)后,底部鍵盤彈起,輸入數(shù)據(jù),輸入框后顯示清空按鈕,點(diǎn)擊ADD,數(shù)據(jù)顯示在底部區(qū)域,數(shù)據(jù)向左滑動(dòng),顯示刪除按鈕,當(dāng)全部刪除后,底部重新顯示空?qǐng)D片。

文件分為三部分:

1、主體:搭UI,實(shí)現(xiàn)方法

2、中間表格部分:表格所需的屬性和方法

3、其他數(shù)據(jù):全局屬性和方法

主體部分:

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

? ? //初始化輸入框和表格

? ? letMyInput =UITextField()

? ? letMyTableView =Mid_List()

? ? lettodomanager =TodoManager()

? ? letMyClick =UIButton()

? ? letMyView =UIView()


? ? overridefuncviewDidLoad() {

? ? ? ? super.viewDidLoad()

? ? ? ? // Do any additional setup after loading the view, typically from a nib.

? ? ? ? //實(shí)例化UI

? ? ? ? MyInput.frame=CGRect(x:contx0, y:conty0, width:contwInput, height:conth)

? ? ? ? MyClick.frame=CGRect(x:contwInput, y:conty0, width:contwClick, height:conth)

? ? ? ? //MyView.frame = CGRect(x:contx0, y: topheight, width: contw, height: midheight)


? ? ? ? self.view.addSubview(MyInput)

? ? ? ? self.view.addSubview(MyClick)

? ? ? ? //self.view.addSubview(MyView)


? ? ? ? //input

? ? ? ? MyInput.borderStyle= .roundedRect

? ? ? ? MyInput.textAlignment = .left

? ? ? ? MyInput.placeholder="在這里輸入"

? ? ? ? MyInput.clearButtonMode = UITextField.ViewMode.whileEditing

? ? ? ? //MyInput.becomeFirstResponder()

? ? ? ? MyInput.delegate=self


? ? ? ? //button

? ? ? ? MyClick.setTitle("ADD", for: .normal)

? ? ? ? MyClick.backgroundColor = UIColor.red

? ? ? ? MyClick.addTarget(self, action:#selector(AddElement), for:UIControl.Event.touchUpInside)


? ? ? ? //table

? ? ? ? MyTableView.view.frame=CGRect(x:contx0, y:topheight, width:contw, height:midheight)

? ? ? ? self.view.addSubview(MyTableView.view)

? ? }


? ??functextFieldShouldReturn(_textField:UITextField) ->Bool{

?? ? ? //當(dāng)點(diǎn)擊鍵盤return時(shí),收回鍵盤

? ? ? ? textField.resignFirstResponder()

? ? ? ? return true

? ? }

? ? //觸發(fā)ADD

?? @objc func AddElement(sender:UIButton){

?? ? ? ? vartmptodos:String! =? MyInput.text

?? ? ? ? MyInput.text=""

?? ? ? ? MyTableView.addtask(cont: tmptodos)

? ? if(MyTableView.contstring.count == 1){

? ? ? ? MyTableView.begininit()

? ? }

? ? }

? ? overridefuncdidReceiveMemoryWarning() {

? ? ? ?super.didReceiveMemoryWarning()

? ? }

} ? ?


2、表格部分:

import?Foundation

import UIKit

class Mid_List:UIViewController,UITableViewDelegate,UITableViewDataSource{

? ? //定義todo內(nèi)容保存數(shù)組

? ? varcontstring = [String]()

? ? let mytablev:UITableView = UITableView()


? ? overridefuncviewDidLoad() {

? ? ? ? super.viewDidLoad()

? ? ? ? //tableview中視圖在UIVIEW之上,定義CGRect的位置是相對(duì)于上層視圖的,即當(dāng)tableview的Y=10,self.view的Y=10,則屏幕上tableview的Y值應(yīng)在20位置上

? ? ? ? mytablev.frame=CGRect(x:0, y:0, width:contw, height:midheight)

? ? ? ? begininit()

? ? ? ? mytablev.delegate=self

? ? ? ? mytablev.dataSource=self

? ? }

? ? //判斷內(nèi)容是否為空,為空顯示圖片,否則顯示list

? ? funcbegininit(){

? ? ? ? if(contstring.count==0){

? ? ? ? ? ? letnullimage =UIImageView(image:UIImage(named:"kongjian"))


? ? ? ? ? ? self.view.addSubview(nullimage)

? ? ? ? }

? ? ? ? else{

? ? ? ? ? ? self.view.addSubview(mytablev)

? ? ? ? }

? ? }

? ? //定義保存動(dòng)作

? ? funcaddtask(cont:String){

? ? ? ? contstring.append(cont)

? ? ? ? print(contstring)

? ? ? ? mytablev.reloadData()

? ? }


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

? ? ? ? return contstring.count

? ? }


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

? ? ? ? letcellid ="testCellID"

? ? ? ? varcell = tableView.dequeueReusableCell(withIdentifier: cellid)

? ? ? ? ifcell ==nil{

? ? ? ? ? ? cell =UITableViewCell(style: .default, reuseIdentifier: cellid)

? ? ? ? }

? ? ? ? cell?.textLabel?.text=contstring[indexPath.row]

? ? ? ? returncell!

? ? }


? ? functableView(_tableView:UITableView, heightForRowAt indexPath:IndexPath) ->CGFloat{

? ? ? ? return44.0

? ? }


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

?? ? ? //print(indexPath.row)


? ? }


? ? //刪除元素

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


? ? ? ? if(editingStyle ==UITableViewCell.EditingStyle.delete){

? ? ? ? ? ? if(contstring.count==1){

? ? ? ? ? ? ? ? self.view.exchangeSubview(at:0, withSubviewAt:1)

? ? ? ? ? ? ? ? print("canyou")

? ? ? ? ? ? }

? ? ? ? ? ? contstring.remove(at: indexPath.row)

? ? ? ? }

?? ? ? ? mytablev.reloadData()

? ? }

}


3、其他數(shù)據(jù):

importFoundation

import UIKit

letcontx0:CGFloat=0

let contw:CGFloat = UIScreen.main.bounds.width

let contwInput:CGFloat = (UIScreen.main.bounds.width)*0.8

let contwClick:CGFloat = (UIScreen.main.bounds.width)*0.2

letconty0:CGFloat=50

letconth:CGFloat=40

let contheight:CGFloat = UIScreen.main.bounds.height

lettopheight:CGFloat=contheight*0.2

letmidheight:CGFloat=contheight*0.8

classTodoManager:NSObject{

? ? vartodos = [String]()

? ? funcaddTask(todo:String){

? ? ? ? todos.append(todo)

? ? }


}

?著作權(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)容

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