一、使用xib
<1>不需要注冊(cè)
直接在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法中寫(xiě)就好了
static NSString *identifer=@"****TableViewCell";
****TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
if (cell == nil) {
? ? ? ? cell = [[[NSBundle mainBundle] loadNibNamed:@"****TableViewCell" owner:self options:nil] lastObject];
?}
<2>需要注冊(cè)
1休雌、在- (void)viewDidLoad;方法中注冊(cè):
[_tableView registerNib:[UINib nibWithNibName:@"****TableViewCell" bundle:nil] forCellReuseIdentifier:@"****TableViewCell"];
2、在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath要销;方法中寫(xiě):
static NSString *identifer=@"****TableViewCell";
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer forIndexPath:indexPath];
二夏块、純代碼
1>疏咐、重寫(xiě)自定義cell的initWithStyle:withReuseableCellIdentifier:方法進(jìn)行布局
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// cell頁(yè)面布局
[self setupView];
}
return self;
}
2>、
1脐供、需要注冊(cè)
為tableView注冊(cè)cell浑塞,使用registerClass:forCellReuseIdentifier:方法注冊(cè)
[_tableView registerClass:[****TableViewCell class] forCellReuseIdentifier:@"****TableViewCell"];
在cellForRowAtIndexPath中使用dequeueReuseableCellWithIdentifier:forIndexPath:獲取重用的cell,若無(wú)重用的cell政己,將自動(dòng)使用所提供的class類(lèi)創(chuàng)建cell并返回
****TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer forIndexPath:indexPath];
2酌壕、不需要注冊(cè)
static NSString *identifer=@"****TableViewCell";
****TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
if (cell == nil) {
cell = [[****TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
}