
这里介绍下tablevIEw的简单使用,大家可以自己在xcode的新建一个项目,然后把下面的代码粘贴到VIEwController文件中,也可以把我给的项目导进去
在这里需要 提到cell的重用机制:当你给出的tablevIEw比较低,一次不足以显示完你的所有cell,所以就需要向下拉,但是拉的时候,最上面的cell就消失了。为了提高效率,swift并没有删除和生成cell,而是下面的cell是复用刚才小时的cell
这里还用到了delegate,其实代理的使用步骤都差不多,大家按照步骤走就行了。如果大家需要理解的更深入一些,可以选择代理,然后按住command键,最后点击一下,就可以到类里面去仔细看了
//// VIEwController.swift// tablevIEwAndCellCode//// Created by wangtuntun on 15/10/8.// copyright (c) 2015年 wangtuntun. All rights reserved.//import UIKitclass VIEwController: UIVIEwController,UItableVIEwDataSource,UItableVIEwDelegate { //继承UItableVIEwDataSource还有UItableVIEwDelegate //定义tablevIEw以及数据源 var tableVIEw1:UItableVIEw! var tableSource=["1","2","3","4","5","k","u","uu","1","uu"]; overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Do any additional setup after loading the vIEw,typically from a nib. println("hello"); //初始化tablevIEw tableVIEw1=UItableVIEw(frame: CGRect(x: 10,y: 10,wIDth: 300,height: 300),style: UItableVIEwStyle.Grouped); tableVIEw1.backgroundcolor=UIcolor.redcolor(); //这是使用监听必须要做的 self.tableVIEw1.delegate=self; self.tableVIEw1.dataSource=self; //这里涉及到cell的重用机制。就是在显示的cell超过tablevIEw时,马上显示的cell重用马上消失的cell// self.tableVIEw1.registerClass(UItableVIEwCell.self,forCellReuseIDentifIEr: "cell"); //将tablevIEw添加代视图中 self.vIEw.addSubvIEw(tableVIEw1); } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated. } // 返回行的个数 func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int{ return tableSource.count; } //返回列的个数 func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int { return 1; } //返回一个cell func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell{ let cellIDentifIEr="homePageIDentifIEr" var cell:UItableVIEwCell!=tableVIEw.dequeueReusableCellWithIDentifIEr(cellIDentifIEr) as! UItableVIEwCell! if cell == nil { cell = UItableVIEwCell(style: .Default,reuseIDentifIEr: cellIDentifIEr) println(indexPath.row); } cell?.textLabel?.text=self.tableSource[indexPath.row]; return cell! // let cell=tableVIEw1.dequeueReusableCellWithIDentifIEr("cell",forIndexPath: indexPath) as! UItableVIEwCell;// cell.textLabel!.text=self.tableSource[indexPath.row];// cell.textLabel?.textcolor=UIcolor.bluecolor();// cell.textLabel?.backgroundcolor=UIcolor.yellowcolor();// return cell; } //返回cell的高度 func tableVIEw(tableVIEw: UItableVIEw,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGfloat{ return 45.0 } //当选中某一行的 *** 作 func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath){ println("\(tableSource[indexPath.row])"); } } 总结 以上是内存溢出为你收集整理的swift中tableview的使用全部内容,希望文章能够帮你解决swift中tableview的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)