Swift中UITableView的用法(自定义UITableView和UITableViewCell)
2016-01-04 21:36
399 查看
一、自定义表视图(UITableView)
import UIKit class HomeTableView: UITableView, UITableViewDataSource, UITableViewDelegate { //表视图的数据源:dataList var dataList = [AnyObject]() //单元格的标识符:homeCellId let identify = "homeCellId" //重写表视图初始化方法 override init(frame: CGRect, style: UITableViewStyle) { super.init(frame:frame, style:style) //调用初始化子视图方法 initSubviews() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } //初始化表视图的子视图方法 func initSubviews () { //设置数据源代理 dataSource = self; //设置表视图代理 delegate = self; //注册单元格class方式 registerClass(HomeCell.self, forCellReuseIdentifier: identify) //设置表视图的分割线显示风格 separatorStyle = .None; //注册单元格xib方式 //registerNib(UINib(nibName: "HomeCell", bundle: nil), forCellReuseIdentifier:identify) } //实现表视图返回组数代理方法:不实现默认为1 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1; } //实现表视图返回行数代理方法 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataList.count; } //实现表视图返回单元格代理方法 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //取单元格 let cell = tableView.dequeueReusableCellWithIdentifier(identify) as! HomeCell //设置单元格的选中风格 cell.selectionStyle = .None; cell.titleStr = dataList[indexPath.row] as? String return cell } //选中单元格的代理方法 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("the indexpath row is \(indexPath.row)") } //返回单元格高度的代理方法 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 80; } }
二、自定义单元格(UITableViewCell)
import UIKit class HomeCell: UITableViewCell { //定义子视图变量 var imgView: UIImageView! var titleLabel: UILabel! var button: UIButton! var titleStr: String? // 重写单元格初始化方法 override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier); //调用单元格初始化方法 initSubviews() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:)has not been implemented") } override func awakeFromNib() { super.awakeFromNib() // Initialization code } //单元格子视图初始化方法 func initSubviews () { imgView = UIImageView(frame: CGRectMake(0, 0, 30, 30)) imgView.image = UIImage(named: "exam.png") contentView.addSubview(imgView); titleLabel = UILabel(frame: CGRectMake(0, 0, frame.size.width, frame.size.height)) titleLabel.textAlignment = .Left; titleLabel.textColor = UIColor.lightGrayColor(); titleLabel.font = UIFont.systemFontOfSize(15); titleLabel.text = "心灵鸡汤,每天一起干" contentView.addSubview(titleLabel) button = UIButton(type: .Custom) button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height) button.setTitle("关注", forState: .Normal) button.setTitleColor(UIColor.whiteColor(), forState: .Normal) button.backgroundColor = UIColor.grayColor() contentView.addSubview(button) } //重写单元格布局子视图方法 override func layoutSubviews() { super.layoutSubviews() titleLabel.text = titleStr //... //布局单元格子视图 //... override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
相关文章推荐
- iOS动态启动界面(swift实现)
- iOS开发之基础控件(Swift版)
- Swift 实现单例模式Singleton pattern的三种方法
- swift学习 运算符 之 for循环
- iOS- Swift:指触即开,如何集成Touch ID指纹识别功能
- 用swift写的两种形式的AlartView
- Swift Tips笔记
- 《swift2.0 官方教程中文版》 第2章-21扩展
- swift安装cocoapod遇到的一些坑
- Swift 之类的继承与类的访问权限
- Swift - Struct 與 Class 的差異性
- 《从零开始学Swift》学习笔记(Day 55)——使用try?和try!区别
- 《从零开始学Swift》学习笔记(Day 55)——使用try?和try!区别
- [ios][swift]使用swift闭包进行viewcontroller反向传值
- (十三) swift 真机调试,ios第三方类库打包
- Swift中的可选类型(Optional)
- Swift可选值OptionalOptional 问号 感叹号
- FM音乐播放器 基于豆瓣 api,swift,alamofire,sqlite.swift 等
- Swift2.1-自动引用计数
- Swift Functional Programming Tutorial