동적 크기의 tableView 셀을 구현하려고합니다. 셀에는 다양한 양의 텍스트를 보유하는 UILabel이 있습니다. 셀을 사용자 정의하기 전에는 cell.textLabel.text를 사용하여 텍스트를 설정하고 셀 크기를 조정하고 모든 텍스트가 표시되었습니다. 나는 레이블을 전혀 엉망으로 만들 필요가 없었습니다. 하지만 이제 셀 내부에 사용자 지정 UILabel이 있고 텍스트가 잘립니다. 나는 여기에서 많은 답변을 보았지만 아무것도 작동하지 않는 것 같습니다.
내 코드는 다음과 같습니다.
//This method gets the size of the text that will be inside the cell/label. This works fine
-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{
CGSize maximumSize = CGSizeMake(labelWidth, 10000);
UIFont *systemFont = [UIFont systemFontOfSize:15.0];
//provide appropriate font and font size
CGSize labelHeightSize = [text sizeWithFont:systemFont
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
return labelHeightSize.height;
}
//Dynamically changes the cell height. This works fine
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reviewText = [[self.reviews objectAtIndex:indexPath.row] objectForKey:@"reviewText"];
CGFloat textHeight = [self getLabelHeightForText:reviewText andWidth:self.tableView.frame.size.width];
return (textHeight + 40);
}
//This is what doesnt seem to be working.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Review label
NSString *reviewText = [[self.reviews objectAtIndex:indexPath.row] objectForKey:@"reviewText"];
UILabel *reviewLabel = (UILabel *)[cell viewWithTag:102];
CGFloat reviewLabelHeight = [self getLabelHeightForText:reviewText andWidth:reviewLabel.frame.size.width];
reviewLabel.frame = CGRectMake(reviewLabel.frame.origin.x, reviewLabel.frame.origin.y, reviewLabel.frame.size.width, reviewLabelHeight);
reviewLabel.text = reviewText;
reviewLabel.numberOfLines = 0;
reviewLabel.lineBreakMode = NSLineBreakByWordWrapping;
NSLog(@"Height: %f", reviewLabel.frame.size.height);
NSLog(@"Width: %f", reviewLabel.frame.size.width);
// cell.textLabel.text = [[self.reviews objectAtIndex:indexPath.row] objectForKey:@"reviewText"];
//
// cell.textLabel.numberOfLines = 0;
//
// cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
return cell;
}
보시다시피, ive는 레이블의 높이와 너비를 기록했습니다. 너비는 정확하고 텍스트에 따라 높이가 동적으로 변경되는 일정한 280을 유지합니다. 모든 것이 잘 작동하는 것처럼 보입니다. 그러나 슬프게도 그렇지 않습니다. 셀 크기가 잘 조정됩니다. 레이블 (NSLogs에서 판단)의 크기가 잘 조정되는 것 같은데 왜 내 텍스트가 잘리나요?
감사
현재 프로토 타입 셀에 몇 가지 자동 레이아웃 제약 조건을 추가하여이 문제를 해결했습니다. 자동 레이아웃을 끄는 것도 작동했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다