tableView的注册机制 ,strong和weak问题
1.经典BUG: 没有使用storyboard加载cell,使用代码创建并且tableview没有注册
unable to dequeue a cell with identifier ID - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
2.解决BUG(BUG出现原因):
1> 只要cell是storyboard中加载或者已经注册的tableviewCell,在dequeueReusableCellWithIdentifier方法中使用forIndexPath没有问题
2> 纯代码在dequeueReusableCellWithIdentifier方法中不能使用forIndexPath
//创建可重用的cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier : resuid forIndexPath :indexPath];
3.dequeueReusableCellWithIdentifier方法在创建cell时底层做的事情
1> 去缓存池里面找有没有id这个标识的cell
2> 去寻找有没有注册cell,有注册cell,就直接用注册的cell类型,创建cell对象
3> 没有注册,返回空
4.使用storyboard加载cell的底层
起始storyboard底层默认会注册一个[UITableViewCell class]类型的cell
5.tableViewCell的注册机制
注册cell,前提条件是只能使用默认样式的cell,使用其他样式要使用xib
[self.tableView registerClass :[UITableViewCell class] forCellReuseIdentifier:resuid];
strong和weak问题
前提:在ARC(自动管理内存)中
1.强引用 __strong
1> 在ARC里面,如果一个对象被强引用就不会被消耗
2> 默认局部变量都是强引用
2.弱引用 __weak
1>好处
one: 节省内存资源
two: 解决strong对strong循环引用
2>控件用weak的原因
view在使用addSubview添加子控件的时候,将控件存进了subviews,由数组管理
数组中所指向的每一个元素都是强引用,所以一直到控制器view销毁,子控件才会被销毁
3>代理用weak的原因
对象是强引用,delegate是必须是弱引用(weak),防止循环引用