網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Swift使用表格組件實現(xiàn)單列表的具體代碼,供大家參考,具體內(nèi)容如下
1、樣例說明:
(1)列表內(nèi)容從Controls.plist文件中讀取,類型為Array 。
(2)點擊列表項會彈出消息框顯示該項信息。
(3)按住列表項向左滑動,會出現(xiàn)刪除按鈕。點擊刪除即可刪除該項。
2、效果圖
3、單元格復(fù)用機制
由于普通的表格視圖中對的單元格形式一般都是相同的,所以本例采用了單元格復(fù)用機制,可以大大提高程序性能。
實現(xiàn)方式是初始化創(chuàng)建 ?UITableView 實例時使用 ?registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell") 創(chuàng)建一個可供重用的 ?UITableViewCell。并將其注冊到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)建一個重用的單元格 ? ? ? ? ?self .tableView!.registerClass( UITableViewCell . self , ? ? ? ? ? ? ?forCellReuseIdentifier: ?"SwiftCell" ) ? ? ? ? ?self .view.addSubview( self .tableView!) ? ? ? ? ? ? ? ? ? ?//創(chuàng)建表頭標簽 ? ? ? ? ?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 = ?"常見 UIKit 控件" ? ? ? ? ?headerLabel.font = ?UIFont .italicSystemFontOfSize(20) ? ? ? ? ?self .tableView!.tableHeaderView = headerLabel ? ? ?} ? ? ? ? ? ?//在本例中,只有一個分區(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ù)使用,在聲明時已注冊 ? ? ? ? ?let ?cell = tableView.dequeueReusableCellWithIdentifier(identify, ? ? ? ? ? ? ?forIndexPath: indexPath) ?as ?UITableViewCell ? ? ? ? ?cell.accessoryType = ?UITableViewCellAccessoryType . DisclosureIndicator ? ? ? ? ?cell.textLabel?.text = ?self .ctrlnames![indexPath.row] ? ? ? ? ?return ?cell ? ? ?} ? ? ? ? ? ?// UITableViewDelegate 方法,處理列表項的選中事件 ? ? ?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 ) ? ? ?} ? ? ? ? ? ?//滑動刪除必須實現(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 ) ? ? ?} ? ? ? ? ? ?//滑動刪除 ? ? ?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() ? ? ?} }
原文鏈接:https://www.hangge.com/blog/cache/detail_552.html
相關(guān)推薦
- 2022-11-26 Python反向傳播實現(xiàn)線性回歸步驟詳細講解_python
- 2022-05-19 C++線程之thread詳解_C 語言
- 2022-12-12 Android?WindowManager深層理解view繪制實現(xiàn)流程_Android
- 2022-05-11 Python學(xué)習(xí)之私有函數(shù),私有變量及封裝詳解_python
- 2023-05-06 如何設(shè)置docker開機自啟動,并設(shè)置容器自動重啟_docker
- 2022-11-05 一文了解Python3的錯誤和異常_python
- 2024-02-26 gitlab合并分支
- 2022-11-04 ASP.NET?MVC使用jQuery的Load方法加載靜態(tài)頁面及注意事項_實用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支