UITableViewCell
의 크기를 조정하여 선택시 세부 텍스트 부분을 표시하거나 숨기려고합니다. 모든 셀이 세부 텍스트의 첫 번째 행을 표시합니다. 사용자가 셀을 탭하면 텍스트의 현재 보이는 부분을 움직이지 않고 전체 텍스트를 표시해야합니다.세부 텍스트에 맞게 UITableViewCell 크기 조정
현재, 내가 뭐하는 거지 다음
didSelectRowAtIndexPath:
if ([self.currentSelection isEqual:indexPath])
{
self.currentSelection = nil;
}
else
{
self.currentSelection = indexPath;
}
[tableView reloadData];
에서
cell.textLabel.text = ...;
cell.detailTextLabel.text = ...;
if ([indexPath isEqual:self.currentSelection])
{
cell.detailTextLabel.numberOfLines = CGFLOAT_MAX;
}
else
{
cell.detailTextLabel.numberOfLines = 1;
}
cellForRowAtIndexPath:
에서 그리고 heightForRowAtIndexPath
float minHeight = 60;
if ([indexPath isEqual:self.currentSelection])
{
KPLexiconEntry *lexiconEntry = ...;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary * attributes = @{NSFontAttributeName : cellFont, NSParagraphStyleAttributeName : paragraphStyle};
CGSize labelSize = [lexiconEntry.explanation boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
float height = labelSize.height + 44;
return height > minHeight ? height : minHeight;
}
return minHeight;
이 표시하거나 세부 텍스트로 숨겨에 나는 원했지만 셀 내부의 텍스트 (textLabel.text
포함)가 몇 픽셀 위 또는 아래로 점프합니다.
어떻게 이것을 방지하고 부드러운 외모를 얻을 수 있습니까?
를? 여전히'heightForRowAtIndexPath'가 필요합니까? – cLar
아니요 CellForRowAtIndex에 넣을 수 있습니다. - (UITableViewCell *) tableView : (UITableView *) tableView cellForRowAtIndexPath : (NSIndexPath *) indexPath { static NSString * cellIdentifire = @ "CellIdentifire"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier : cellIdentifire]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : cellIdentifire]; [cell.textLabel sizeToFit]; } cell.textLable.text = @ "" "; 반환 셀; } –
빠른 응답을 보내 주셔서 감사합니다. 나는 그렇게하려고 시도했지만, 이제 선택 영역의 크기가 조정되지 않습니다. – cLar