博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift 纯代码tableView
阅读量:7007 次
发布时间:2019-06-27

本文共 2181 字,大约阅读时间需要 7 分钟。

hot3.png

cell 

class DCTableViewCell: UITableViewCell {        lazy var dcLabel: UILabel = {        let dcLabel = UILabel()        dcLabel.text = "百度"        dcLabel.backgroundColor = UIColor.greenColor()        dcLabel.textAlignment = NSTextAlignment.Center        return dcLabel    }()        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {        super.init(style: style, reuseIdentifier: reuseIdentifier)                        addSubview(dcLabel);        dcLabel.frame = CGRectMake(0, 0, 100, 80)        dcLabel.text = "赵大财博客";            }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }    }

VC

import UIKitclass ViewController: UIViewController {        lazy var tableView : UITableView = {        return UITableView()    }()        override func viewDidLoad() {        super.viewDidLoad()                setupUI()    }}// MARK:- 设置UI界面相关extension ViewController {    /// 设置UI界面    func setupUI() {        // 0.将tableView添加到控制器的View中        view.addSubview(tableView)                // 1.设置tableView的frame        tableView.frame = view.bounds                // 2.设置数据源        tableView.dataSource = self                // 3.设置代理        tableView.delegate = self                //注册CELL        tableView.registerClass(DCTableViewCell.self, forCellReuseIdentifier:"celli")                    }}// MARK:- tableView的数据源和代理方法// extension类似OC的category,也是只能扩充方法,不能扩充属性extension ViewController : UITableViewDataSource, UITableViewDelegate{    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return 20    }        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        let cell = tableView.dequeueReusableCellWithIdentifier("celli");                        return cell!            }        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {        print("点击了:\(indexPath.row)")    }            func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {        return 80    }}

 

转载于:https://my.oschina.net/zhaodacai/blog/725002

你可能感兴趣的文章
CGLib动态代理原理及实现
查看>>
selenium模拟鼠标悬停操作
查看>>
mysql事务,select for update,及数据的一致性处理
查看>>
java8-Lambda
查看>>
Bash 实例,第 2 部分
查看>>
eclipse JavaEE版"javax.servlet.http.HttpServlet" was not found on the Java Build Path问题的解决办法...
查看>>
【crowdin】快速国际化
查看>>
CSS3实现背景颜色渐变 摘抄
查看>>
CentOS 6.8 安装vnc
查看>>
阿里云提供的一个监测cpu和内存的shell脚本(get_cpu_mem_info.sh)
查看>>
怎么让input不失去焦点
查看>>
jsp中getParameter与getAttribute的区别
查看>>
【ME - Mail】之Apache Commons Email
查看>>
应用组件
查看>>
angular2 学习二 {{model}} - 文本插值
查看>>
Android postDelay+Dialog引起的窗体泄露
查看>>
PHP 初试多线程pthreads扩展
查看>>
centos7 virtualbox 设置静态ip
查看>>
2.3 Shared Libraries
查看>>
Mac修改MySQL编码
查看>>