大跃进了...基础很重要....
大多App,展示数据主要还是用到了UITableView
, 那是作为数据展示的重要组成部分 cell
就显得尤为关键.
- 自定义
cell
的好处
- 好处一 : .....
- 好处二 : .....
UITableViewCell
缓存池
cell
的要显示到用户能看到的屏幕上, 必然的要创建出来, 用户看不到了, 那也要必然的销毁掉
so, 问题来了:
频繁的创建与销毁, 对于移动设备来说, 这完全就是浪费, 最重要的是苹果设备的内存就那么大点, iPhone6S 之前内存只有1G, 如果我们自己的App需要展示大量的数据, 再加上频繁的创建销毁cell
,我已经看到了闪退的瞬间...
基于此类原因, 有一种解决方法诞生了, 它就是缓存池(重用机制)
, 关于缓存池的原理, 相信很多人都知道...但是我还是要继续说下去...
屏幕就那么大, 用户看到的数据是有限的,
cell
最多也就不过20+吧
那么当cell
离开屏幕的时候就是要被销毁的时候, 我们可以不让这个cell
被销毁掉, 可以把它放到一边去, 当我们需要用的时候, 可以直接使用这个没有被销毁掉的cell
而这个一边去, 就是缓存池
, 缓存池可以有N多个, 这就是为什么需要identifier
, 原因就是我们可以通过identifier
来找到响应的缓存池
图片展示地址:...http://www.jianshu.com/p/e25391c1c431
代码部分
xib没法沾上来, 不过Xib中也没有任何操作
// 自定义Cell文件: ZQTestCell.swift
import UIKit
class ZQTestCell: UITableViewCell {
// Xib 初始化
override func awakeFromNib()
{
super.awakeFromNib()
self.backgroundColor = UIColor.redColor()
}
// Class 初始化
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.backgroundColor = UIColor.blueColor()
}
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)!
}
// CSDN上查的
// 添加如下构造函数 (普通初始化)
// override init() { }
// 如果控制器需要通过xib加载,则需要添加
// required init(coder aDecoder: NSCoder) {}
}
Main.storyboard文件也没法粘上来...
Main.storyboard
添加了个UITableViewController
,
还拖拽一个Table View Cell
控件, 设置了Identifier
及背景颜色
// ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource
{
//MARK: 控件区
@IBOutlet weak var tableView: UITableView!
//MARK: cell 标识符
let CellIdentifierNib = "Nib"
let CellIdentifierClass = "Class"
let CellIdentifierStoryboard = "Storyboard"
let CellIdentifierdSystem = "System"
//MARK: 方法区
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.darkGrayColor()
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
// Nib 注册
self.tableView.registerNib(UINib(nibName: "ZQTestCell", bundle: nil), forCellReuseIdentifier: CellIdentifierNib)
// Class 注册
self.tableView.registerClass(ZQTestCell.self, forCellReuseIdentifier: CellIdentifierClass)
}
//MARK: 数据源
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 4
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
// storyboard 中 拖进去的 cell, 需要在 Identifier 属性赋值’A’, 当tableView需要循环使用该cell的时候 Identifier 就是’A’
if (indexPath.row == 0)
{
var cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifierStoryboard)
if (cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: CellIdentifierStoryboard)
}
cell?.textLabel?.text = "Storyboard"
return cell!
}
// Nib注册, 调用的时候会调用自定义cell中的 awakeFromNib 方法
if (indexPath.row == 1)
{
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifierNib)
cell?.textLabel?.text = "nib"
return cell!
}
// Class, 调用 init(style: UITableViewCellStyle, reuseIdentifier: String?)
if (indexPath.row == 2)
{
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifierClass)
cell?.textLabel?.text = "class"
return cell!
}
// system
var cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifierdSystem)
if (cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: CellIdentifierdSystem)
}
cell!.textLabel?.text = "System"
cell?.textLabel?.textColor = UIColor.whiteColor()
cell?.backgroundColor = UIColor.blackColor()
return cell!
}
}
问题搜索地址
Stackoverflow
http://stackoverflow.com/questions/26613053/initcoder-has-not-been-implemented-in-swift (表示看不太懂)
网友评论