網站首頁 編程語言 正文
本文實例為大家分享了swift表格控件的簡單使用,供大家參考,具體內容如下
1、效果圖
2、該控件(UITableView) 代碼注意的地方:
A、ViewController 不單單繼承于 UIViewController,還有 UITableViewDelegate,UITableViewDataSource。
B、要自己重新實現UITableView的3個方法。分別是:numberOfSectionInTableView(table:UITableView), tableView(table:UITableView, numberOfRowsInSetion section:Int), tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
3、我使用的Xcode版本:7.2;
4、完整 源碼
import UIKit class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource{ ? ?? ? ? let ctrlName = ["按鈕", "標簽", "文本框", "提示框", "開關按鈕"]; ? ? var tableView:UITableView?; ? ?? ? ? override func loadView() { ? ? ? ? super.loadView(); ? ? } ? ?? ? ? override func viewDidLoad() { ? ? ? ? super.viewDidLoad() ? ? ? ?? ? ? ? ? /// 創建視圖 ? ? ? ? tableView = UITableView(frame: view.frame, style: .Plain); ? ? ? ? tableView!.dataSource = self; ? ? ? ? tableView!.delegate = self; ? ? ? ?? ? ? ? ? tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell"); ? ? ? ? view.addSubview(tableView!); ? ? ? ?? ? ? ? ?? ? ? ? ? /// 創建標簽 ? ? ? ? let headerLabel = UILabel(frame: CGRectMake(0, 0, 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); ? ? ? ?? ? ? ? ? /// 在tableView的頂部添加該label ? ? ? ? tableView!.tableHeaderView = headerLabel; ? ? } ? ? /// 設置分區 ? ? func numberOfSectionsInTableView(tableView: UITableView) -> Int { ? ? ? ? return 1; ? ? } ? ?? ? ? /// 返回表格行數 ? ? func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ? ? ? ? return ctrlName.count; ? ? } ? ?? ? ? /// 創建單元格顯示內容,(創建indexPath指定的單元) ? ? func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { ? ? ? ?? ? ? ? ? /// 為了提高表格顯示性能,以創建完成的單元重復使用 ? ? ? ? let identify = "SwiftCell"; ? ? ? ? /// 同一形式的單元格重復使用,在聲明時已注冊 ? ? ? ? let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath); ? ? ? ? cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator; ? ? ? ? cell.textLabel?.text = ctrlName[indexPath.row]; ? ? ? ?? ? ? ? ? return cell; ? ? } ? ?? ? ? /// UITableViewDelegate方法,處理列表項選中事件 ? ? func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { ? ? ? ?? ? ? ? ? tableView.deselectRowAtIndexPath(indexPath, animated: true); ? ? ? ? let itemString = ctrlName[indexPath.row]; ? ? ? ?? ? ? ? ? /// 創建提示框 ? ? ? ? let alertView = UIAlertController(title: "提示", message: "你選中了\(itemString)", preferredStyle: .Alert); ? ? ? ? /// 向提示框中增加按鈕 ? ? ? ? let alertAction = UIAlertAction(title: "確定", style: UIAlertActionStyle.Default, handler: {(action)->Void in}); ? ? ? ? alertView.addAction(alertAction); ? ? ? ?? ? ? ? ? presentViewController(alertView, 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/50785693
相關推薦
- 2022-09-16 C++中的位運算和位圖bitmap解析_C 語言
- 2023-12-13 記錄choice函數用法
- 2022-12-29 Kotlin?Lambda表達式實踐使用介紹_Android
- 2022-03-29 深入了解Python并發編程_python
- 2022-12-27 golang中日期操作之日期格式化及日期轉換_Golang
- 2023-01-20 React?useMemo與useCallabck有什么區別_React
- 2022-04-19 C語言內存管理及初始化細節示例詳解_C 語言
- 2023-04-22 python統計函數被調用次數的實現_python
- 最近更新
-
- 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同步修改后的遠程分支