網站首頁 編程語言 正文
本文實例為大家分享了swift自定義表格控件的具體代碼,供大家參考,具體內容如下
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]? = ["按鈕", "文本框", "標簽"]; ? ?? ? ? var allnames:Dictionary<Int, [String]>? ? ?? ? ? var adHeaders:[String]? ? ?? ? ? var ctype:UIControlType! ? ?? ? ? override func loadView() { ? ? ? ? super.loadView() ? ? } ? ?? ? ? override func viewDidLoad() { ? ? ? ? super.viewDidLoad() ? ? ? ?? ? ? ? ? // ? ? ? ?//初始化數據,這一次數據,我們放在屬性列表文件里 ? ? ? ? // ? ? ? ?self.ctrlnames = ?NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("Controls", ofType:"plist")!) as? Array ? ? ? ? // ? ? ? ? // ? ? ? ?print(self.ctrlnames, terminator: "") ? ? ? ?? ? ? ? ? //初始化數據,這一次數據,我們放在屬性列表文件里 ? ? ? ? self.allnames = ?[ 0:[String](self.ctrlnames!),1:[String]([ ? ? ? ? ? ? "日期選擇器", ? ? ? ? ? ? "網頁選擇器", ? ? ? ? ? ? "工具條", ? ? ? ? ? ? "表格視圖"]) ? ? ? ? ]; ? ? ? ?? ? ? ? ? // ? ? ? ?print(self.allnames, terminator: "") ? ? ? ?? ? ? ? ? self.adHeaders = [ ? ? ? ? ? ? "常見UIKit控件", ? ? ? ? ? ? "高級UIKit控件" ? ? ? ? ] ? ? } ? ?? ? ? @IBAction func generalBtnClicked(sender: UIButton) { ? ? ? ? self.ctype = UIControlType.Basic ? ? ? ?? ? ? ? ?? ? ? ? ? //創建表視圖 ? ? ? ? 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 ? ? ? ? //創建一個重用的單元格 ? ? ? ? self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell") ? ? ? ? self.view.addSubview(self.tableView!) ? ? ? ?? ? ? ? ?? ? ? ? ? //創建表頭標簽 ? ? ? ? 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 ? ? ? ?? ? ? ? ? //創建表視圖 ? ? ? ?? ? ? ? ?? ? ? ? ? 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 ? ? ? ? //創建一個重用的單元格 ? ? ? ? self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell") ? ? ? ? self.view.addSubview(self.tableView!) ? ? ? ?? ? ? ? ? //創建表頭標簽 ? ? ? ? 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 ? ? } ? ?? ? ?? ? ? //在本例中,只有一個分區 ? ? func numberOfSectionsInTableView(tableView: UITableView) -> Int { ? ? ? ? return self.ctype == UIControlType.Basic ? 1:2; ? ? } ? ?? ? ? //返回表格行數(也就是返回控件數) ? ? func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ? ? ? ? let data = self.allnames?[section] ? ? ? ? return data!.count ? ? } ? ?? ? ?? ? ? // UITableViewDataSource協議中的方法,該方法的返回值決定指定分區的頭部 ? ? func tableView(tableView:UITableView, titleForHeaderInSection ? ? ? ? section:Int)->String? ? ? { ? ? ? ? var headers = ?self.adHeaders!; ? ? ? ? return headers[section]; ? ? } ? ? // UITableViewDataSource協議中的方法,該方法的返回值決定指定分區的尾部 ? ? func tableView(tableView:UITableView, titleForFooterInSection ? ? ? ? section:Int)->String? ? ? { ? ? ? ? let data = self.allnames?[section] ? ? ? ? return "有\(data!.count)個控件" ? ? } ? ?? ? ?? ? ? //創建各單元顯示內容(創建參數indexPath指定的單元) ? ? func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell ? ? { ? ? ? ? let identify:String = "SwiftCell"; ? ? ? ?? ? ? ? ? /// 同一形式的單元格重復使用。 ? ? ? ? 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. ? ? } }
原文鏈接:https://blog.csdn.net/HK_5788/article/details/50808625
相關推薦
- 2022-04-12 一篇文章帶你了解python中的typing模塊和類型注解_python
- 2022-06-02 GO中?分組聲明與array,?slice,?map函數_Golang
- 2022-05-19 聊聊Python代碼中if?__name__?==?‘__main__‘的作用是什么_python
- 2022-09-08 執行go?vendor第三方包版本沖突問題解決_Golang
- 2022-09-22 ansible-playbook 可用參數
- 2024-02-29 UNI-APP項目在引用官方提供的Uni-App-Demo實例中的組件時應該注意的問題
- 2022-07-14 React父子組件傳值(組件通信)的實現方法_React
- 2022-02-24 詳細整理Oracle中常用函數_oracle
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支