方案1
1.首先在主方法里面注册Cell.
UINib *cellNib = [UINib nibWithNibName:@"C1" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"C1"];
self.prototypeCell= [self.tableView dequeueReusableCellWithIdentifier:@"C1"];
2.返回正常的cell,记住此时不需要创建cell.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
C1 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C1"];
cell.t.text = [self.tableData objectAtIndex:indexPath.row];
return cell;}
3.在返回高度方法返回高度.
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
C1 *cell = (C1 *)self.prototypeCell;
cell.translatesAutoresizingMaskIntoConstraints = NO;
cell.t.translatesAutoresizingMaskIntoConstraints = NO;
cell.i.translatesAutoresizingMaskIntoConstraints = NO;
cell.t.text = [self.tableData objectAtIndex:indexPath.row];
CGSize size=[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
NSLog(@"h=%f", size.height + 1);
return 1 + size.height;}
iOS7中系统先调用:
-(CGFloat)tableView:(UITableView)tableView heightForRowAtIndexPath:(NSIndexPath )indexPath这个方法,
再调用**
-(UITableViewCell *)tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath 。
** 这个方法。iOS8恰好相反。