swift纯代码实现UITableview总结一
来源:互联网 发布:路由器网络不稳定 编辑:程序博客网 时间:2024/04/29 23:36
在swift中没有了宏定义这个概念,所以在获取屏幕宽高时,可以设置其为常量
let SRC_WIDTH =UIScreen.mainScreen().bounds.width
let SRC_HEIGHT =UIScreen.mainScreen().bounds.height
swfit创建UITableView和OC中差不多,主要是语法发生了改变的,还有一点就是拆解包问题,不过一般编译器会给出提示
你要实现UiTableView,肯定就少不了要实现它的相关代理方法,在swift中不需要尖括号,只需要在继承的类后面用逗号隔开就可以来,如下所示:
class TableViewController:UIViewController,UITableViewDataSource,UITableViewDelegate{},这是系统会报错,不用管,因为你还没有实现它的代理和协议的;
声明UITableView
var tableView: UITableView?
在初始化UITableView,如下:
/** * 创建UITableView */ func tableViewTest() -> UITableView{ if tableView == nil{ tableView = UITableView(frame: CGRectMake(0, 0, SRC_WIDTH, SRC_HEIGHT), style: UITableViewStyle.Plain) tableView?.delegate = self tableView?.dataSource = self tableView?.showsHorizontalScrollIndicator = false tableView?.showsVerticalScrollIndicator = false tableView?.bounces = false tableView?.tableFooterView = UIView(frame: CGRectMake(0,0,0,0))<span style="white-space:pre"></span>//去除多余的下划线 } return tableView! }
UITableView已经创建好了,接下来的就是要实现它的数据源方法的:
分别是:
//返回cell的显示的数据
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{}
//返回当前的Section中row行数
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int{}
//返回当前几个Section
func numberOfSectionsInTableView(tableView:UITableView) ->Int { }
//选中时方法
func tableView(tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath){}
显示数据源方法如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("CELL") if cell == nil { cell = UITableViewCell(style: .Default, reuseIdentifier: "CELL") } cell!.selectionStyle = UITableViewCellSelectionStyle.None cell!.textLabel?.text = items![indexPath.row] as? String return cell! }
- swift纯代码实现UITableview总结一
- IOS-UITableView上的纯代码实现
- 纯代码实现自定义UITableView的cell
- swift4.0 UITableView纯代码实现
- swift中实现UITableView总结二
- swift+UIPageViewController 纯代码实现引导页
- AutoLayout自定义tableViewCell --- Masonry + UITableView+FDTemplateLayoutCell 纯代码实现
- swift UITableView(一)
- iOS UITableView(一)-纯代码创建UITableView,Cell点击事件,Cell左滑删除
- Swift-UITableView的实现
- swift----纯代码实现 UITableViewCell单元格的自定义
- Swift - 纯代码实现页面segue跳转,以及参数传递
- Swift - 纯代码实现页面segue跳转,以及参数传递
- Swift ☞ UITableView(一)
- Swift纯代码走进UICollectionView
- swift纯代码创建view
- UI023---代码实现UITableView
- 一 swift之UICollectionView纯代码cell的复用、footerView的添加
- C语言常用知识点
- 邮件系统收不到外部邮件域名的配置问题
- Oracle的REGEXP_REPLACE函数简单用法
- 银行家算法(Banker's Algorithm)
- C# 打印PDF文件之使用不同打印机打印所有页面或部分页面
- swift纯代码实现UITableview总结一
- vs问题小记
- LINUX最大线程数及最大进程数
- common-long包简介
- Oracle设置和删除归档日志
- 40个Java多线程问题总结
- [转载]辨析access_ok put_user __put_user get_user __get_user
- Type mismatch: cannot convert from Integer to int(java 报错)
- 普通程序员通向架构师的最佳路径?