SDK 6.0 TableView Cell重用
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
NS_AVAILABLE_IOS(6_0); // newer
第一个:
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
在初始化时候用:
static
NSString
*CellIdentifier = @
"Cell"
;
if
(cell ==
nil
) {
cell = [[
UITableViewCell
alloc] initWithStyle:
UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
//设置你的cell
}
第二个 : - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
根据说明文档。要用注册办法
// 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);
这里用
- (
void
)registerClass:(Class)cellClass forCellReuseIdentifier:(
NSString
*)identifier 注册
在TabelViewController出事化时候加入
self
.backgroundColor = xxxx;
[
self
registerClass:[CustomCell
class
] forCellReuseIdentifier:@
"CustomCell"
];
这样在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath时候就可以省下
static
NSString
*CellIdentifier = @
"Cell"
;
if
(cell ==
nil
) {
cell = [[
UITableViewCell
alloc] initWithStyle:
UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
//设置你的cell
}