UITableView中registerClass: forCellReuseIdentifier:的用法

你已经用NIB做了一个Cell,或者自定义了一个Cell。我们在你创建UITableView的时候,就可以顺带

 

 self.tableView.backgroundColor = xxxx;

 [self.tableView 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

 }

 而只需要

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

参考原文:http://www.zpluz.com/thread-3504-1-1.html 

原文地址:https://www.cnblogs.com/NINIiOS/p/4571138.html