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

swift自定義表格控件(UITableView)

 更新時(shí)間:2022年01月27日 08:37:46   作者:PandaMohist  
這篇文章主要為大家詳細(xì)介紹了swift自定義表格控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了swift自定義表格控件的具體代碼,供大家參考,具體內(nèi)容如下

1、效果圖

2、控件

storyboard上的控件就2個(gè):UIButton。

3、為按鈕添加點(diǎn)擊事件

通過(guò)輔助編輯器為這2個(gè)按鈕添加按鈕單擊事件:分別為 generalBtnClick 和   groupBtnClick

4、完整代碼

import UIKit

enum UIControlType{
? ? case Basic
? ? case Advanced
}

class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
? ??
? ? var tableView:UITableView?
? ??
? ? var ctrlnames:[String]? = ["按鈕", "文本框", "標(biāo)簽"];
? ??
? ? var allnames:Dictionary<Int, [String]>?
? ??
? ? var adHeaders:[String]?
? ??
? ? var ctype:UIControlType!
? ??
? ? 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, terminator: "")
? ? ? ??
? ? ? ? //初始化數(shù)據(jù),這一次數(shù)據(jù),我們放在屬性列表文件里
? ? ? ? self.allnames = ?[ 0:[String](self.ctrlnames!),1:[String]([
? ? ? ? ? ? "日期選擇器",
? ? ? ? ? ? "網(wǎng)頁(yè)選擇器",
? ? ? ? ? ? "工具條",
? ? ? ? ? ? "表格視圖"])
? ? ? ? ];
? ? ? ??
? ? ? ? // ? ? ? ?print(self.allnames, terminator: "")
? ? ? ??
? ? ? ? self.adHeaders = [
? ? ? ? ? ? "常見(jiàn)UIKit控件",
? ? ? ? ? ? "高級(jí)UIKit控件"
? ? ? ? ]
? ? }
? ??
? ? @IBAction func generalBtnClicked(sender: UIButton) {
? ? ? ? self.ctype = UIControlType.Basic
? ? ? ??
? ? ? ??
? ? ? ? //創(chuàng)建表視圖
? ? ? ? self.tableView = UITableView(frame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100), 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
? ? }
? ??
? ? @IBAction func groupBtnClicked(sender: UIButton) {
? ? ? ? self.ctype = UIControlType.Advanced
? ? ? ??
? ? ? ? //創(chuàng)建表視圖
? ? ? ??
? ? ? ??
? ? ? ? self.tableView = UITableView(frame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100), style:UITableViewStyle.Grouped)
? ? ? ? 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 = "高級(jí) UIKit 控件"
? ? ? ? headerLabel.font = UIFont.italicSystemFontOfSize(20)
? ? ? ? self.tableView!.tableHeaderView = headerLabel
? ? }
? ??
? ??
? ? //在本例中,只有一個(gè)分區(qū)
? ? func numberOfSectionsInTableView(tableView: UITableView) -> Int {
? ? ? ? return self.ctype == UIControlType.Basic ? 1:2;
? ? }
? ??
? ? //返回表格行數(shù)(也就是返回控件數(shù))
? ? func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
? ? ? ? let data = self.allnames?[section]
? ? ? ? return data!.count
? ? }
? ??
? ??
? ? // UITableViewDataSource協(xié)議中的方法,該方法的返回值決定指定分區(qū)的頭部
? ? func tableView(tableView:UITableView, titleForHeaderInSection
? ? ? ? section:Int)->String?
? ? {
? ? ? ? var headers = ?self.adHeaders!;
? ? ? ? return headers[section];
? ? }
? ? // UITableViewDataSource協(xié)議中的方法,該方法的返回值決定指定分區(qū)的尾部
? ? func tableView(tableView:UITableView, titleForFooterInSection
? ? ? ? section:Int)->String?
? ? {
? ? ? ? let data = self.allnames?[section]
? ? ? ? return "有\(zhòng)(data!.count)個(gè)控件"
? ? }
? ??
? ??
? ? //創(chuàng)建各單元顯示內(nèi)容(創(chuàng)建參數(shù)indexPath指定的單元)
? ? func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
? ? {
? ? ? ? let identify:String = "SwiftCell";
? ? ? ??
? ? ? ? /// 同一形式的單元格重復(fù)使用。
? ? ? ? let secno = indexPath.section;
? ? ? ? var data = self.allnames?[secno];
? ? ? ? if (0 == secno)
? ? ? ? {
? ? ? ? ? ? let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath);
? ? ? ? ? ? cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator;
? ? ? ? ? ??
? ? ? ? ? ? cell.imageView?.image = UIImage(named: "1");
? ? ? ? ? ? cell.textLabel?.text = data![indexPath.row];
? ? ? ??
? ? ? ? ? ? return cell;
? ? ? ? }
? ? ? ??
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? let adcell = UITableViewCell(style: .Subtitle, reuseIdentifier: "SwiftCell");
? ? ? ? ? ? adcell.textLabel?.text = data![indexPath.row];
? ? ? ? ? ? adcell.detailTextLabel?.text = "這是\(data![indexPath.row])的說(shuō)明";
? ? ? ? ? ??
? ? ? ? ? ? return adcell;
? ? ? ? }
? ? }
? ??
? ? // UITableViewDelegate 方法,處理列表項(xiàng)的選中事件
? ? func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
? ? {
? ? ? ? self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
? ? ? ??
? ? ? ? let itemString = self.ctrlnames![indexPath.row]
? ? ? ??
? ? ? ? let ?alert = UIAlertController(title: "提示", message: "你選擇了:\(itemString)", preferredStyle: UIAlertControllerStyle.Alert);
? ? ? ? let sureAction = UIAlertAction(title: "確定", style: UIAlertActionStyle.Default, handler: {(action)->Void in});
? ? ? ? alert.addAction(sureAction);
? ? ? ??
? ? ? ? presentViewController(alert,animated:true, completion:nil);
? ? ? ??
? ? }
? ??
? ? override func didReceiveMemoryWarning() {
? ? ? ? super.didReceiveMemoryWarning()
? ? ? ??
? ? ? ? // Dispose of any resources that can be recreated.
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • swift實(shí)現(xiàn)自定義圓環(huán)進(jìn)度提示效果

    swift實(shí)現(xiàn)自定義圓環(huán)進(jìn)度提示效果

    這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)自定義圓環(huán)進(jìn)度提示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Swift 數(shù)組及常用方法詳解總結(jié)

    Swift 數(shù)組及常用方法詳解總結(jié)

    數(shù)組(Array)是有序的元素序列。 若將有限個(gè)類型相同的變量的集合命名,那么這個(gè)名稱為數(shù)組名。組成數(shù)組的各個(gè)變量稱為數(shù)組的分量,也稱為數(shù)組的元素,有時(shí)也稱為下標(biāo)變量
    2021-11-11
  • 詳解Swift?中的幻象類型

    詳解Swift?中的幻象類型

    讓我們來(lái)看看一種技術(shù),它可以讓我們利用?Swift?的類型系統(tǒng)在編譯時(shí)執(zhí)行更多種類的數(shù)據(jù)驗(yàn)證消除更多潛在的歧義來(lái)源,并幫助我們?cè)谡麄€(gè)代碼庫(kù)中保持類型安全,通過(guò)使用幻象類型(phantom?types),對(duì)Swift?幻象類型相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-05-05
  • Swift data范圍截取問(wèn)題解決方案

    Swift data范圍截取問(wèn)題解決方案

    這篇文章主要介紹了Swift data范圍截取問(wèn)題解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Swift UILable 設(shè)置內(nèi)邊距實(shí)例代碼

    Swift UILable 設(shè)置內(nèi)邊距實(shí)例代碼

    本文主要介紹Swift UILable 設(shè)置內(nèi)邊距,這里提供示例代碼供大家參考,有需要的小伙伴可以看下
    2016-07-07
  • 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
  • Spring中BeanFactory與FactoryBean的區(qū)別解讀

    Spring中BeanFactory與FactoryBean的區(qū)別解讀

    這篇文章主要介紹了Spring中BeanFactory與FactoryBean的區(qū)別解讀,Java的BeanFactory是Spring框架中的一個(gè)接口,它是用來(lái)管理和創(chuàng)建對(duì)象的工廠接口,在Spring中,我們可以定義多個(gè)BeanFactory來(lái)管理不同的組件,需要的朋友可以參考下
    2023-12-12
  • Swift開發(fā)之UITableView狀態(tài)切換效果

    Swift開發(fā)之UITableView狀態(tài)切換效果

    這篇文章主要介紹了Swift開發(fā)之UITableView狀態(tài)切換效果的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08
  • Swift教程之基本運(yùn)算符詳解

    Swift教程之基本運(yùn)算符詳解

    這篇文章主要介紹了Swift教程之基本運(yùn)算符詳解,本文講解了賦值運(yùn)算符、數(shù)學(xué)運(yùn)算符、取余運(yùn)算符、浮點(diǎn)余數(shù)計(jì)算、自增和自減運(yùn)算符等,需要的朋友可以參考下
    2015-01-01
  • 在Swift程序中實(shí)現(xiàn)手勢(shì)識(shí)別的方法

    在Swift程序中實(shí)現(xiàn)手勢(shì)識(shí)別的方法

    這篇文章主要介紹了在Swift程序中實(shí)現(xiàn)手勢(shì)識(shí)別的方法,蘋果的Swift語(yǔ)言即將進(jìn)入2.0開源階段,人氣爆棚中:D 需要的朋友可以參考下
    2015-07-07

最新評(píng)論

盘山县| 卢氏县| 信宜市| 扶绥县| 靖安县| 崇义县| 江山市| 乐东| 扶沟县| 昌图县| 桐柏县| 威信县| 威信县| 囊谦县| 云阳县| 兴海县| 巨鹿县| 无锡市| 丰县| 景泰县| 无为县| 上饶县| 商丘市| 武宣县| 邵东县| 保康县| 东丰县| 抚松县| 昌江| 彩票| 桂平市| 龙泉市| 通化市| 宜川县| 万山特区| 嵊州市| 镇巴县| 苏尼特右旗| 驻马店市| 九江市| 英吉沙县|