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

swift自定義表格控件(UITableView)

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

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

1、效果圖

2、控件

storyboard上的控件就2個:UIButton。

3、為按鈕添加點擊事件

通過輔助編輯器為這2個按鈕添加按鈕單擊事件:分別為 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)頁選擇器",
? ? ? ? ? ? "工具條",
? ? ? ? ? ? "表格視圖"])
? ? ? ? ];
? ? ? ??
? ? ? ? // ? ? ? ?print(self.allnames, terminator: "")
? ? ? ??
? ? ? ? self.adHeaders = [
? ? ? ? ? ? "常見UIKit控件",
? ? ? ? ? ? "高級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)建一個重用的單元格
? ? ? ? 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 = "常見 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)建一個重用的單元格
? ? ? ? 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 = "高級 UIKit 控件"
? ? ? ? headerLabel.font = UIFont.italicSystemFontOfSize(20)
? ? ? ? self.tableView!.tableHeaderView = headerLabel
? ? }
? ??
? ??
? ? //在本例中,只有一個分區(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)個控件"
? ? }
? ??
? ??
? ? //創(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])的說明";
? ? ? ? ? ??
? ? ? ? ? ? return adcell;
? ? ? ? }
? ? }
? ??
? ? // UITableViewDelegate 方法,處理列表項的選中事件
? ? 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.
? ? }
}

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

相關(guān)文章

  • 詳解Swift面向?qū)ο缶幊讨械姆椒?method)

    詳解Swift面向?qū)ο缶幊讨械姆椒?method)

    既然面向?qū)ο竽蔷鸵欢〞衜ethod,方法和面向過程語言中的function函數(shù)并沒什么區(qū)別,只不過方法在面向?qū)ο笳Z言中可以被類來約束作用域,這里我們就來詳解Swift面向?qū)ο缶幊讨械姆椒?method)
    2016-07-07
  • swift表格控件使用方法詳解(UITableview)

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

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

    Swift自定義UITableViewCell背景色

    這篇文章主要為大家詳細(xì)介紹了Swift自定義UITableViewCell背景色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 深入理解Swift語言中的閉包機(jī)制

    深入理解Swift語言中的閉包機(jī)制

    這篇文章主要介紹了Swift語言中的閉包機(jī)制,是Swift入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11
  • swift語言AutoreleasePool原理及使用場景

    swift語言AutoreleasePool原理及使用場景

    這篇文章主要為大家介紹了swift語言AutoreleasePool原理及使用場景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 利用Swift實現(xiàn)一個響應(yīng)式編程庫

    利用Swift實現(xiàn)一個響應(yīng)式編程庫

    最近在學(xué)習(xí)swift,最近有空所以總結(jié)一下最近學(xué)習(xí)的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于利用Swift實現(xiàn)一個響應(yīng)式編程庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • 用Swift構(gòu)建一個簡單的iOS郵件應(yīng)用的方法

    用Swift構(gòu)建一個簡單的iOS郵件應(yīng)用的方法

    這篇文章主要介紹了用Swift構(gòu)建一個簡單的iOS郵件應(yīng)用的方法,包括查看和標(biāo)記已讀等基本的郵件應(yīng)用功能,需要的朋友可以參考下
    2015-07-07
  • Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解

    Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解

    這篇文章主要給大家介紹了關(guān)于Swift中defer關(guān)鍵字推遲執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • Swift 使用 Observe 監(jiān)測頁面滾動的實現(xiàn)方法

    Swift 使用 Observe 監(jiān)測頁面滾動的實現(xiàn)方法

    這篇文章主要介紹了Swift 使用 Observe 監(jiān)測頁面滾動的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • Swift里的值類型與引用類型區(qū)別和使用

    Swift里的值類型與引用類型區(qū)別和使用

    這篇文章主要介紹了Swift里的值類型與引用類型區(qū)別和使用,本文講解了值類型與引用類型的區(qū)別、如何選擇類型、什么時候該用值類型、什么時候該用引用類型等內(nèi)容,需要的朋友可以參考下
    2015-05-05

最新評論