Swift使用表格組件實(shí)現(xiàn)單列表
本文實(shí)例為大家分享了Swift使用表格組件實(shí)現(xiàn)單列表的具體代碼,供大家參考,具體內(nèi)容如下
1、樣例說(shuō)明:
(1)列表內(nèi)容從Controls.plist文件中讀取,類型為Array 。
(2)點(diǎn)擊列表項(xiàng)會(huì)彈出消息框顯示該項(xiàng)信息。
(3)按住列表項(xiàng)向左滑動(dòng),會(huì)出現(xiàn)刪除按鈕。點(diǎn)擊刪除即可刪除該項(xiàng)。
2、效果圖
3、單元格復(fù)用機(jī)制
由于普通的表格視圖中對(duì)的單元格形式一般都是相同的,所以本例采用了單元格復(fù)用機(jī)制,可以大大提高程序性能。
實(shí)現(xiàn)方式是初始化創(chuàng)建 UITableView 實(shí)例時(shí)使用 registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell") 創(chuàng)建一個(gè)可供重用的 UITableViewCell。并將其注冊(cè)到UITableView,ID為 SwiftCell。
下次碰到形式(或結(jié)構(gòu))相同的單元就可以直接使用UITableView的dequeueReusableCellWithIdentifier 方法從UITableView中取出。
4、示例代碼
--- ViewController.swift ---
import ?UIKit ? class ?ViewController : ?UIViewController , ?UITableViewDelegate , ?UITableViewDataSource ?{ ? ? ? ? ? ?var ?ctrlnames:[ String ]? ? ? ?var ?tableView: UITableView ? ? ? ? ? ? ?override ?func ?loadView() { ? ? ? ? ?super .loadView() ? ? ?} ? ? ? ? ? ?override ?func ?viewDidLoad() { ? ? ? ? ?super .viewDidLoad() ? ? ? ? ? ? ? ? ? ?//初始化數(shù)據(jù),這一次數(shù)據(jù),我們放在屬性列表文件里 ? ? ? ? ?self .ctrlnames = ? NSArray (contentsOfFile: ? ? ? ? ? ? ?NSBundle .mainBundle().pathForResource( "Controls" , ofType: "plist" )!) ?as ? ?Array ? ? ? ? ? ? ? ? ? ?print ( self .ctrlnames) ? ? ? ? ? ? ? ? ? ?//創(chuàng)建表視圖 ? ? ? ? ?self .tableView = ?UITableView (frame: ?self .view.frame, style: UITableViewStyle . Plain ) ? ? ? ? ?self .tableView!.delegate = ?self ? ? ? ? ?self .tableView!.dataSource = ?self ? ? ? ? ?//創(chuàng)建一個(gè)重用的單元格 ? ? ? ? ?self .tableView!.registerClass( UITableViewCell . self , ? ? ? ? ? ? ?forCellReuseIdentifier: ?"SwiftCell" ) ? ? ? ? ?self .view.addSubview( self .tableView!) ? ? ? ? ? ? ? ? ? ?//創(chuàng)建表頭標(biāo)簽 ? ? ? ? ?let ?headerLabel = ?UILabel (frame: ?CGRectMake (0, 0, ?self .view.bounds.size.width, 30)) ? ? ? ? ?headerLabel.backgroundColor = ?UIColor .blackColor() ? ? ? ? ?headerLabel.textColor = ?UIColor .whiteColor() ? ? ? ? ?headerLabel.numberOfLines = 0 ? ? ? ? ?headerLabel.lineBreakMode = ?NSLineBreakMode . ByWordWrapping ? ? ? ? ?headerLabel.text = ?"常見(jiàn) UIKit 控件" ? ? ? ? ?headerLabel.font = ?UIFont .italicSystemFontOfSize(20) ? ? ? ? ?self .tableView!.tableHeaderView = headerLabel ? ? ?} ? ? ? ? ? ?//在本例中,只有一個(gè)分區(qū) ? ? ?func ?numberOfSectionsInTableView(tableView: ?UITableView ) -> ?Int ?{ ? ? ? ? ?return ?1; ? ? ?} ? ? ? ? ? ?//返回表格行數(shù)(也就是返回控件數(shù)) ? ? ?func ?tableView(tableView: ?UITableView , numberOfRowsInSection section: ?Int ) -> ?Int ?{ ? ? ? ? ?return ?self .ctrlnames!.count ? ? ?} ? ? ? ? ? ?//創(chuàng)建各單元顯示內(nèi)容(創(chuàng)建參數(shù)indexPath指定的單元) ? ? ?func ?tableView(tableView: ?UITableView , cellForRowAtIndexPath indexPath: ?NSIndexPath ) ? ? ? ? ?-> ?UITableViewCell ? ? ?{ ? ? ? ? ?//為了提供表格顯示性能,已創(chuàng)建完成的單元需重復(fù)使用 ? ? ? ? ?let ?identify: String ?= ?"SwiftCell" ? ? ? ? ?//同一形式的單元格重復(fù)使用,在聲明時(shí)已注冊(cè) ? ? ? ? ?let ?cell = tableView.dequeueReusableCellWithIdentifier(identify, ? ? ? ? ? ? ?forIndexPath: indexPath) ?as ?UITableViewCell ? ? ? ? ?cell.accessoryType = ?UITableViewCellAccessoryType . DisclosureIndicator ? ? ? ? ?cell.textLabel?.text = ?self .ctrlnames![indexPath.row] ? ? ? ? ?return ?cell ? ? ?} ? ? ? ? ? ?// UITableViewDelegate 方法,處理列表項(xiàng)的選中事件 ? ? ?func ?tableView(tableView: ?UITableView , didSelectRowAtIndexPath indexPath: ?NSIndexPath ) ? ? ?{ ? ? ? ? ?self .tableView!.deselectRowAtIndexPath(indexPath, animated: ?true ) ? ? ? ? ? ? ? ? ? ?let ?itemString = ?self .ctrlnames![indexPath.row] ? ? ? ? ? ? ? ? ? ?let ?alertController = ?UIAlertController (title: ?"提示!" , ? ? ? ? ? ? ?message: ?"你選中了【\(itemString)】" , preferredStyle: . Alert ) ? ? ? ? ?let ?okAction = ?UIAlertAction (title: ?"確定" , style: . Default ,handler: ?nil ) ? ? ? ? ?alertController.addAction(okAction) ? ? ? ? ?self .presentViewController(alertController, animated: ?true , completion: ?nil ) ? ? ?} ? ? ? ? ? ?//滑動(dòng)刪除必須實(shí)現(xiàn)的方法 ? ? ?func ?tableView(tableView: ?UITableView , ? ? ? ? ?commitEditingStyle editingStyle: ?UITableViewCellEditingStyle , ? ? ? ? ?forRowAtIndexPath indexPath: ?NSIndexPath ) { ? ? ? ? ? ? ?print ( "刪除\(indexPath.row)" ) ? ? ? ? ? ? ?let ?index = indexPath.row ? ? ? ? ? ? ?self .ctrlnames?.removeAtIndex(index) ? ? ? ? ? ? ?self .tableView?.deleteRowsAtIndexPaths([indexPath], ? ? ? ? ? ? ? ? ?withRowAnimation: ?UITableViewRowAnimation . Top ) ? ? ?} ? ? ? ? ? ?//滑動(dòng)刪除 ? ? ?func ?tableView(tableView: ?UITableView , ? ? ? ? ?editingStyleForRowAtIndexPath indexPath: ?NSIndexPath ) ? ? ? ? ?-> ?UITableViewCellEditingStyle ?{ ? ? ? ? ? ? ?return ?UITableViewCellEditingStyle . Delete ? ? ?} ? ? ? ? ? ?//修改刪除按鈕的文字 ? ? ?func ?tableView(tableView: ?UITableView , ? ? ? ? ?titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: ?NSIndexPath ) ? ? ? ? ?-> ?String ? { ? ? ? ? ? ? ?return ?"刪" ? ? ?} ? ? ? ? ? ?override ?func ?didReceiveMemoryWarning() { ? ? ? ? ?super .didReceiveMemoryWarning() ? ? ?} }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01swift中利用runtime交換方法的實(shí)現(xiàn)示例
這篇文章主要給大家介紹了關(guān)于swift中利用runtime交換方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-05-05深入探究Swift枚舉關(guān)聯(lián)值的內(nèi)存
這篇文章主要給大家介紹了關(guān)于Swift枚舉關(guān)聯(lián)值的內(nèi)存的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Swift使用CollectionView實(shí)現(xiàn)廣告欄滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Swift使用CollectionView實(shí)現(xiàn)廣告欄滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06關(guān)于Swift 4.1中的Codable改進(jìn)詳解
這篇文章主要給大家介紹了關(guān)于Swift 4.1中的Codable改進(jìn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02