最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年01月27日 09:19:36   作者:hangge  
這篇文章主要為大家詳細(xì)介紹了Swift使用表格組件實(shí)現(xiàn)單列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

  • iOS Swift UICollectionView橫向分頁(yè)滾動(dòng),cell左右排版問(wèn)題詳解

    iOS Swift UICollectionView橫向分頁(yè)滾動(dòng),cell左右排版問(wèn)題詳解

    UICollectionView是iOS中比較常見(jiàn)的一個(gè)控件,這篇文章主要給大家介紹了關(guān)于iOS Swift UICollectionView橫向分頁(yè)滾動(dòng),cell左右排版問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • Swift?中?Opaque?Types學(xué)習(xí)指南

    Swift?中?Opaque?Types學(xué)習(xí)指南

    這篇文章主要為大家介紹了Swift?中?Opaque?Types學(xué)習(xí)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • swift中的@UIApplicationMain示例詳解

    swift中的@UIApplicationMain示例詳解

    這篇文章主要給大家介紹了關(guān)于swift中@UIApplicationMain的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • 簡(jiǎn)單了解Swift語(yǔ)言中的break和continue語(yǔ)句的用法

    簡(jiǎn)單了解Swift語(yǔ)言中的break和continue語(yǔ)句的用法

    這篇文章主要簡(jiǎn)單介紹了Swift語(yǔ)言中的break和continue語(yǔ)句的用法,與其他語(yǔ)言的一樣用于循環(huán)語(yǔ)句流程控制,需要的朋友可以參考下
    2015-11-11
  • Swift中循環(huán)語(yǔ)句中的轉(zhuǎn)移語(yǔ)句 break 和 continue

    Swift中循環(huán)語(yǔ)句中的轉(zhuǎn)移語(yǔ)句 break 和 continue

    這篇文章主要介紹了Swift中循環(huán)語(yǔ)句中的轉(zhuǎn)移語(yǔ)句 break 和 continue,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的的朋友參考下
    2016-12-12
  • Swift實(shí)現(xiàn)“或”操作符的3種方法示例

    Swift實(shí)現(xiàn)“或”操作符的3種方法示例

    這篇文章主要給大家介紹了關(guān)于Swift實(shí)現(xiàn)“或”操作符的3種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Swift3.0剪切板代碼拷貝及跨應(yīng)用粘貼實(shí)現(xiàn)代碼

    Swift3.0剪切板代碼拷貝及跨應(yīng)用粘貼實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Swift3.0剪切板代碼拷貝及跨應(yīng)用粘貼的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Swift實(shí)現(xiàn)監(jiān)聽(tīng)鍵盤(pán)通知及一些處理詳解

    Swift實(shí)現(xiàn)監(jiān)聽(tīng)鍵盤(pán)通知及一些處理詳解

    這篇文章主要給大家介紹了關(guān)于Swift實(shí)現(xiàn)監(jiān)聽(tīng)鍵盤(pán)通知及一些處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • Swift中排序算法的簡(jiǎn)單取舍詳解

    Swift中排序算法的簡(jiǎn)單取舍詳解

    對(duì)于排序算法, 通常簡(jiǎn)單的, 為大家所熟知的有, 選擇排序, 冒泡排序, 快速排序, 當(dāng)然還有哈希, 桶排序之類的, 本文僅比較最為常見(jiàn)的選擇, 冒泡和快排,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2018-03-03
  • swift表格控件使用方法詳解(UITableview)

    swift表格控件使用方法詳解(UITableview)

    這篇文章主要為大家詳細(xì)介紹了swift表格控件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01

最新評(píng)論

新闻| 卓资县| 宁陵县| 察雅县| 江城| 元朗区| 南宁市| 隆子县| 永修县| 丰县| 临城县| 扎兰屯市| 张家口市| 靖州| 留坝县| 晋宁县| 商洛市| 陈巴尔虎旗| 南投县| 合肥市| 游戏| 岳池县| 郴州市| 甘南县| 武汉市| 仙游县| 洛阳市| 如东县| 昭通市| 瑞昌市| 海阳市| 改则县| 嘉义市| 灵丘县| 灵川县| 综艺| 平果县| 额尔古纳市| 洮南市| 米林县| 平罗县|