3
다양한 행 높이의 테이블 뷰가 있습니다. 셀의 높이는 각 셀의 텍스트 양을 기준으로합니다. 아래쪽으로 확장 할 수 있도록 레이블에 꼬리 끌기, 선행 및 맨 위 제한이있는 레이블이 IB에서 만들어집니다. 내 앱을 테스트 할 때 첫 번째 셀은 좋지만 한 번 스크롤하면 모든 것이 엉망이되기 시작합니다. 셀 높이가 꺼져 있고 셀이 겹치는 것처럼 보입니다.UITableView 일부 셀이 겹치고 있습니다.
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat ht=[self heightForText:your text];
}
당신을 희망 : 사용 후
-(CGFloat)heightForText:(NSString *)text
{
UITextView * textView = [[UITextView alloc] initWithFrame: CGRectMake(x, y, width, height)];
textView.text = text;
textView.font = [UIFont fontWithName:@"Helvetica" size:15];
[textView sizeToFit];
return textView.frame.size.height;
}
이 방법 셀의 높이를 계산 :
-(TableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
postCell = [TableView dequeueReusableCellWithIdentifier:@"Cell"];
if (postCell == nil) {
postCell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
postCell.label.text = [array objectAtIndex:indexPath.row];
return postCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *textString = [array objectAtIndex:indexPath.row];
CGRect frame = [textString boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 69, 1000.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil];
return ceil(frame.size.height) + 80;
}
이는 이전과 동일한 결과를 제공합니다. 모든 세포는 마지막 두 개 또는 세 개의 세포를 제외하면 좋다. – bpomp87
모든 문자열에 대해이 메서드를 사용해야하며 모든 문자열을 사용할 때 높이를 가져야합니다. 이 높이를 색인 생성 경로의 높이로 설정하십시오. – Jashu