//Array of name
NSMutableArray *nameArray;
//take outlet of height constraint in custom cell;
@property(weak) IBOutlet NSLayoutConstraint *labelHeightConstraint;
-(void)viewDidLoad
{
_nameArray=[NSMutableArray arrayWithObjects:,@"I am using Auto-layout. In Custom UITableViewCell added a UILabel with max width.",@"Hi I am Rohit",@"I Like To Help Society" ,nil];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomerRatingsCell *customerRatingsCell=[tableView dequeueReusableCellWithIdentifier:@"CustomerRatingsIdentifier"];
customerRatingsCell.labelName=[_nameArray objectAtIndex:indexPath.row];
customerRatingsCell.headerLabelConstraint=[self getLabelHeight:[NSString stringWithFormat:@"%@",[_nameArray objectAtIndex:indexPath.row]]];
return CustomerRatingsCell;
}
및
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self getLabelHeight:[NSString stringWithFormat:@"%@",[_nameArray objectAtIndex:indexPath.row]]]+25 ;//plus 25 is remaining space apart from label height, if you are using the full height label equals to cell height then don't add it.
}
- (CGFloat)getLabelHeight:(NSString*)textLabel
{
CGRect textRect = [textLabel boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:Avenir Roman size:12.0f]} context:nil];
CGSize expectedLabelSize = CGSizeMake(textRect.size.width, textRect.size.height);
return expectedLabelSize.height;
}
당신이 제약의 어떤 종류가 있습니까를 화면의 현재 상태의 사진을 첨부? 그 라벨의 부모보기는 무엇입니까? 여러 줄로 된 텍스트입니까? –
@ JulianKról : 'UITableViewCell'이 (가) 상위입니다. 'UILabel'의 numberoflines 속성보기가 0입니다. 나는 $ 꼬리, 꼬리, 위, 아래 공간을 포함하고있다. – coreDeviOS