
创建Cell
1) 在DemoListCell.xib中画出你想要的cell样式(autoLayout),另外注意要给Cell制定 IDentityID: DemoListID
设置CellID
2) 我这里创建了两个UIImage,一个UILabel (图片我会后续补上)
创建布局控件
3) 从UIDemoListCell.xib 向 UIDemoListCell.swift 划线(右键选择控件不放拖到.swift文件中放手并命名),Cell样式的初始化就完成了,接下来我们需要调用。代码如下:
swift@IBOutlet weak var cellimg: UIImageVIEw!@IBOutlet weak var cellLabel: UILabel!@IBOutlet weak var cellicon: UIImageVIEw!
注释:图片我会后续补上去
调用自定义的UItableVIEwCell
1) 创建数据源和CellID
swiftlet cellID = "DemoListID" //获取CellIDvar tableData: (Titles:[String],values:[String])? //定义一个数据源
2) 在vIEwDIDLoad中使用代码创建一个UItableVIEw
swiftoverrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() self.Title = "主页" self.vIEw.backgroundcolor = UIcolor.whitecolor() //demoList的设置 self.demoList.frame = CGRectMake(0,0,self.vIEw.frame.wIDth,self.vIEw.frame.height) //下面代码是用来去掉UItableVIEw的Cell之间的线 //self.demoList.separatorStyle = UItableVIEwCellSeparatorStyle.None let nib = UINib(nibname: "DemoListCell",bundle: nil) //nibname指的是我们创建的Cell文件名 self.demoList.registerNib(nib,forCellReuseIDentifIEr: cellID) self.demoList.delegate = self self.demoList.dataSource = self self.vIEw.addSubvIEw(self.demoList) self.showData() }展示数据源,这里我就写一个静态数据作为数据源即可
swiftfunc showData() { self.tableData = (["SLC提示组件","SwiftNotice组件--JohnLui","CNPPopup组件","闭包回调","KLCPopup组件","Pitaya网络组件","Neon布局组件"],["SCLAlert","SwiftNotice","CNPPopup","ClosureBack","",""]) self.demoList.reloadData()}
既然使用了UItableVIEw那么就必须要使用注意到有些必须的代理需要重写,其实我们可以去UItableVIEw中查看,没有 optional开头的function都是必须重写
1) 这里我们重写 这里我们重写四个,代码如下:
swiftfunc numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int { return 1 } func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { guard let count:Int = self.tableData!.Titles.count else { print("没有数据") } return count } func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { let cell = tableVIEw.dequeueReusableCellWithIDentifIEr(self.cellID,forIndexPath: indexPath) as! DemoListCell //cell.cellimg.image = UIImage(named: powerData[indexPath.row][2]) cell.cellLabel.text = self.tableData!.Titles[indexPath.row] return cell } func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) { let index = indexPath.row let storyID = tableData!.values[index] as String let storyboard = UIStoryboard(name: "Main",bundle: nil) var nextVIEw:UIVIEwController switch storyID { case "SCLAlert": nextVIEw = storyboard.instantiateVIEwControllerWithIDentifIEr(storyID) as! SCLAlertDemoVIEwController case "SwiftNotice": nextVIEw = storyboard.instantiateVIEwControllerWithIDentifIEr(storyID) as! SwiftNoticeDemoVIEwController case "CNPPopup": nextVIEw = storyboard.instantiateVIEwControllerWithIDentifIEr(storyID) as! CNPPopupDemoVIEwController case "ClosureBack": nextVIEw = LWRootVIEwController() default: nextVIEw = storyboard.instantiateVIEwControllerWithIDentifIEr("SCLAlert") as! SCLAlertDemoVIEwController } self.navigationController?.pushVIEwController(nextVIEw,animated: true) } func tableVIEw(tableVIEw: UItableVIEw,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGfloat { return 50 }注意事项:
既然创建了UItableVIEw,需要在Class继承后面加上Delegate和DataSource
swiftclass MainVIEwController: UIVIEwController,UItableVIEwDelegate,UItableVIEwDataSource总结:
这样我们就成功创建了一个纯代码创建的UItableVIEw以及调用了自定义的Cell,是不是很简单!
下面就是效果图(我的GitHub )
以上是内存溢出为你收集整理的Swift编程(一):UITableView及自定义Cell的Xib全部内容,希望文章能够帮你解决Swift编程(一):UITableView及自定义Cell的Xib所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)