Swift 2.0 UItableView 的简单使用
在IOS开发中,UItableView 的使用真的是最常见最普通的了,现在在自学swift 今天也是这用Swift 写了写 UItableview的使用,还有一些经常出错的地方。下面我先把整个控制器的代理列出来,大家可以顺便看看 swift 是怎样遵守协议的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | import UIKit // 遵守两个协议 class HomeViewController : UIViewController , UITableViewDelegate , UITableViewDataSource { override func viewDidLoad () { super . viewDidLoad () self . view . backgroundColor = UIColor . whiteColor () // 创建tableview creatTableview () // Do any additional setup after loading the view. } // 创建tableView的方法 func creatTableview () { let tableview : UITableView = UITableView ( frame : self . view . frame , style : UITableViewStyle . Plain ) tableview . delegate = self tableview . dataSource = self // 这里要不注册,用下面的 dequeueReusableCellWithIdentifier 这个方法的时候会崩溃,这里和大家说一下,dequeueReusableCellWithIdentifier("SwiftCell", forIndexPath: indexPath) 要使用这个方法,在这里你就必须得先注册一个复用的cell,然后使用的时候 dequeueReusableCellWithIdentifier 会去调用能复用的cell tableview . registerClass ( UITableViewCell . self , forCellReuseIdentifier : "SwiftCell" ) self . view . addSubview ( tableview ) } // 组数 func numberOfSectionsInTableView ( tableView : UITableView ) - > Int { return 3 } // 每组的个数 func tableView ( tableView : UITableView , numberOfRowsInSection section : Int ) - > Int { switch section { case 0 : return 5 case 1 : return 3 case 2 : return 4 default : return 0 } } func tableView ( tableView : UITableView , cellForRowAtIndexPath indexPath : NSIndexPath ) - > UITableViewCell { let cell : UITableViewCell = tableView . dequeueReusableCellWithIdentifier ( "SwiftCell" , forIndexPath : indexPath ) cell . textLabel ?. text = "你真的很帅" return cell } func tableView ( tableView : UITableView , heightForHeaderInSection section : Int ) - > CGFloat { return 20 } // tableView是否能编辑,true 返回,就是能编辑 func tableView ( tableView : UITableView , shouldIndentWhileEditingRowAtIndexPath indexPath : NSIndexPath ) - > Bool { return true } // 你要删除cell的时候,这个方法会自己调用的,这里一般就是在数据源里面删除cell 对应的数据。 func tableView ( tableView : UITableView , commitEditingStyle editingStyle : UITableViewCellEditingStyle , forRowAtIndexPath indexPath : NSIndexPath ) { print ( "删了这条数据了" ) } // 这里是编辑的类型 func tableView ( tableView : UITableView , editingStyleForRowAtIndexPath indexPath : NSIndexPath ) - > UITableViewCellEditingStyle { return UITableViewCellEditingStyle . Delete } // 这里是给 删除按钮上的 按钮文字进行重命名,这里是改成“别删呀”的名字。 func tableView ( tableView : UITableView , titleForDeleteConfirmationButtonForRowAtIndexPath indexPath : NSIndexPath ) - > String ? { return "别删呀" } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } |
特别说明几点内容:
1.自定义cell时,
若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib
不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
2.需不需要注册?
使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;
使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。
## 努力做一个合格的程序员。
分类:
Swift 学习笔记
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话