UITableViewCell高度自适应
// iOS8之后,我们只需要设置这两句代码之后,即可放心的往cell的控件里面加上内容,cell会根据内部所有控件的高度动态的计算自己的高度从而显示出来
tableView.estimatedRowHeight = 44.0f;//推测高度,必须有,可以随便写多少
tableView.rowHeight =UITableViewAutomaticDimension;//iOS8之后默认就是这个值,可以省略
// 如果有部分cell高度是固定的,部分cell高度需要自适应,可以这样写
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return 60;
} else {
return UITableViewAutomaticDimension;
}
}
复用cell注册写法
//(1)系统cell
//1.注册
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//2.不注册
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
//(2)自定义cell
//1.注册
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"cell"];
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//2.不注册
CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
//(3)自定义cell Xib
//1.注册
[tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
//2.不注册
xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil] lastObject];
}