RZMars个人Blog地址: http://www.rzmars.com
皆さん、こんにちは。把今晚遇到的问题贴出来。どうぞよろしくお願いします
在tableView: cellForRowAtIndexPath:方法中有两个可以获得重用cell的方法;
分别是:- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
// Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// newer dequeue method guarantees a cell is returned and resized properly,
//assuming identifier is regist
我们可以发现第二个方法的的注释已经说明这个方法是ios6.0之后才又的,那么我们低版本使用的话肯定会产生问题的了,不过还好抛出的异常已经告诉了我们答案:
//reason: ’unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard’
很明显,做android开发的人一看到这个可能又点想法了,因为在android当中也又类似的模式,动态加载布局文件嘛!
那么我们应该如何在ios5.0之前取正确的使用这个方法呢?我们需要结合一下两个方法中的任一个去配套的完成这正确的使用这个方法,方法如下:
// Beginning in iOS 6, clients can register a nib or class for each cell. // If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned. // Instances returned from the new dequeue method will also be properly sized when they are returned. - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0); - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0); 这两个方法分别是使用加载类与nib文件的。我们需要在那里使用他们呢? 假如我们要自定义一个Tableview的话 我们可以在这个控间的init 或者viewdidload方法中去实现这个方法,代码如下:[
self
.tableView registerClass:[CustomCell
class
] forCellReuseIdentifier:@
"CustomCell"
];
这样我们就可以在 tableview中的cellForRowAtIndexPath方法中调用这个- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath这个方法了。 你的明白?