日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Swift使用表格組件實現(xiàn)單列表_Swift

作者:hangge ? 更新時間: 2022-04-08 編程語言

本文實例為大家分享了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

欄目分類
最近更新