欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • Swift教程之控制流詳解

    Swift教程之控制流詳解

    這篇文章主要介紹了Swift教程之控制流詳解,本文詳細(xì)講解了Swift中的for循環(huán)、for-in循環(huán)、For-Condition-Increment條件循環(huán)、while循環(huán)、Do-while循環(huán)、if條件語(yǔ)句等控制流語(yǔ)句,需要的朋友可以參考下
    2015-01-01
  • Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器項(xiàng)目

    Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器項(xiàng)目

    這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 解析Swift中的泛型支持與使用

    解析Swift中的泛型支持與使用

    支持泛型意味著可以在規(guī)定參數(shù)類型的情況下更靈活地編寫程序,也是Swift語(yǔ)言先進(jìn)而又強(qiáng)大的體現(xiàn),這里我們就來(lái)解析Swift中的泛型支持與使用:
    2016-07-07
  • Swift?Error重構(gòu)優(yōu)化詳解

    Swift?Error重構(gòu)優(yōu)化詳解

    這篇文章主要為大家介紹了Swift?Error的問(wèn)題解決及重構(gòu)優(yōu)化方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • swift中利用runtime交換方法的實(shí)現(xiàn)示例

    swift中利用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 hello world!Swift快速入門教程

    Swift hello world!Swift快速入門教程

    這篇文章主要介紹了Swift hello world!Swift快速入門教程,本文在快速了解Swift編程語(yǔ)言,需要的朋友可以參考下
    2014-07-07
  • 深入探究Swift枚舉關(guān)聯(lián)值的內(nèi)存

    深入探究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-08
  • Swift使用CollectionView實(shí)現(xiàn)廣告欄滑動(dòng)效果

    Swift使用CollectionView實(shí)現(xiàn)廣告欄滑動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了Swift使用CollectionView實(shí)現(xiàn)廣告欄滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Swift心得筆記之運(yùn)算符

    Swift心得筆記之運(yùn)算符

    區(qū)別于 C 語(yǔ)言,在 Swift 中你可以對(duì)浮點(diǎn)數(shù)進(jìn)行取余運(yùn)算(%),Swift 還提供了 C 語(yǔ)言沒(méi)有的表達(dá)兩數(shù)之間的值的區(qū)間運(yùn)算符,(a..b和a...b),這方便我們表達(dá)一個(gè)區(qū)間內(nèi)的數(shù)值。
    2015-04-04
  • 關(guān)于Swift 4.1中的Codable改進(jìn)詳解

    關(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

最新評(píng)論