swift3使用协议和泛型简化UITableView的使用
来源:互联网 发布:马东眼袋在哪割的 知乎 编辑:程序博客网 时间:2024/06/05 19:31
普通我们使用UITableView时我们总是要定义一个ID,然后注册
tableview.register(cellClass: AnyClass?, forCellReuseIdentifier: String)
tableView.dequeueReusableCell(withIdentifier: String, for: IndexPath)
定义、注册、使用时我们会用到三次CellReuseIdentifier,我们通过用协议和泛型简化UITableView的使用,即不再需要这三次的Identifier
实现步骤
1、写协议
//
// ReusableCell.swift
// reusecell
//
// Created by targetcloud on 2017/3/26.
// Copyright © 2017年 targetcloud. All rights reserved.
//
import UIKit
protocol ReusableCell : class{
static var reuseableIdentifier :String {get}
}
extension ReusableCell where Self : UITableViewCell{
static var reuseableIdentifier :String{
return "\(self)"
}
}
extension UITableView{
func registerCell<T : UITableViewCell >(_ cell :T.Type)where T : ReusableCell{
register(cell, forCellReuseIdentifier: T.reuseableIdentifier)
}
func dequeReusableCell<T : UITableViewCell>(indexPath : IndexPath) ->T where T : ReusableCell{
return dequeueReusableCell(withIdentifier: T.reuseableIdentifier, for: indexPath)as! T
}
}
2、自定义Cell,Cell遵守上面的协议
//
// TGCell.swift
// reusecell
//
// Created by targetcloud on 2017/3/26.
// Copyright © 2017年 targetcloud. All rights reserved.
//
import UIKit
class TGCell: UITableViewCell,ReusableCell {
}
3、使用
//
// ViewController.swift
// reusecell
//
// Created by targetcloud on 2017/3/26.
// Copyright © 2017年 targetcloud. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let tv = UITableView(frame: view.bounds)
tv.dataSource = self
tv.registerCell(TGCell.self)
view.addSubview(tv)
}
}
extension ViewController :UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeReusableCell(indexPath: indexPath) as TGCell
cell.textLabel?.text = " --- ReusableCell \(indexPath.row) --- "
return cell
}
}
从上面看到,我们不需要再关心ID了
当然协议还要能够适应XIB的注册,那么新的协议如下代码
1、修改协议
//
// ReusableCell.swift
// reusecell
//
// Created by targetcloud on 2017/3/26.
// Copyright © 2017年 targetcloud. All rights reserved.
//
import UIKit
protocol ReusableCell : class{
static var reuseableIdentifier :String {get}
static var nib : UINib? {get}
}
extension ReusableCell where Self : UITableViewCell{
static var reuseableIdentifier :String{
//return "\(self)"
//两种实现方式都可以
return String(describing: self)
}
static var nib : UINib? {
return nil
}
}
extension UITableView{
func registerCell<T : UITableViewCell >(_ cell : T.Type) where T : ReusableCell{
if let nib = T.nib{
register(nib, forCellReuseIdentifier: T.reuseableIdentifier)
}else{
register(cell, forCellReuseIdentifier: T.reuseableIdentifier)
}
}
func dequeReusableCell<T : UITableViewCell>(indexPath : IndexPath) -> T where T : ReusableCell{
return dequeueReusableCell(withIdentifier: T.reuseableIdentifier, for: indexPath) as! T
}
}
2、自定义NIB(或者叫XIB)
//
// TGNibCell.swift
// reusecell
//
// Created by targetcloud on 2017/3/26.
// Copyright © 2017年 targetcloud. All rights reserved.
//
import UIKit
class TGNibCell: UITableViewCell,ReusableCell {
@IBOutlet weak var nibTestLabel: UILabel!
static var nib : UINib?{
return UINib(nibName: "TGNibCell", bundle: nil)
}
}
3、使用
//
// ViewController.swift
// reusecell
//
// Created by targetcloud on 2017/3/26.
// Copyright © 2017年 targetcloud. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let tv = UITableView(frame: view.bounds)
tv.dataSource = self
//tv.registerCell(TGCell.self)
tv.registerCell(TGNibCell.self)
view.addSubview(tv)
}
}
extension ViewController :UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
/*
let cell = tableView.dequeReusableCell(indexPath: indexPath) as TGCell
cell.textLabel?.text = " --- ReusableCell \(indexPath.row) --- "
*/
let cell = tableView.dequeReusableCell(indexPath: indexPath)as TGNibCell
cell.nibTestLabel?.text = " --- ReusableCellFromNib \(indexPath.row) --- "
return cell
}
}
- swift3使用协议和泛型简化UITableView的使用
- swift3使用协议和泛型简化UITableView的使用(续)多种类型的cell注册
- swift3 fileprivate和open的使用
- Swift3 - 字符串的使用
- UITableView的创建和使用
- Swift3使用GCD和DispatchQueues
- swift3.0 coredata 的使用
- Swift3.0 popToViewController 的使用
- Swift3-UISearchController的基本使用
- Swift3.0 popToViewController 的使用
- swift3.0:sqlite3的使用
- FCKeditor 2.0的简化和使用
- ShareSDK的简化压缩和使用例子
- FMDB的基本使用和解析(Swift3.0)
- 三、swift3.0字符串和集合的基本使用
- swift3.0之UIImagePickerController的使用和注意事项
- Swift3.0之hidesBottomBarWhenPushed的使用和注意事项
- JDBC之使用SimpleJdbcTemplate和泛型技术简化代码
- WordPress插件 Pdf Premium PFD完美嵌入式专业版插件[更新至3.0.1]
- WordPress插件 WooCommerce最新微信支付接口集成插件
- 《用python写网络爬虫》--识别网站所用技术
- 大整数类
- css3的pointer-events属性
- swift3使用协议和泛型简化UITableView的使用
- Unity3D说明文档翻译-Mobile Keyboard
- Leetcode 21 Merge Two Sorted Lists
- Cocos Lua入坑过程
- Unity3D说明文档翻译-Graphics
- 动态化加载已安装的apk
- JQuery中serialize()用法实例分析
- Unity3D说明文档翻译-Physics
- elasticsearch集群安装与使用