2014-11-18 3 views
2

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 포함)가 몇 픽셀 위 또는 아래로 점프합니다.
어떻게 이것을 방지하고 부드러운 외모를 얻을 수 있습니까?

답변

0

내가이 해결하는 방법을 알아 낸 :
대신의 detailLabel를 사용하여, 나는 NSAttributedString을 작성 textLabel.attributedText을 사용했다. 속성이 지정된 문자열은 단순히 나머지 (이전에 detailLabel에 있음)보다 큰 글꼴로 된 제목 (이전의 textLabel)을 보여줍니다. 나는 기인 문자열을 만들 수있는 방법을 만들어 : 당신이 볼 수 있듯이

- (NSAttributedString*)attributedTextForIndexPath:(NSIndexPath*)indexPath { 
    id itemForIndexPath = ...; 
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init]; 

    UIFont *titleFont = [UIFont systemFontOfSize:16]; 
    NSDictionary *titleAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor clearColor], NSFontAttributeName: titleFont}; 
    NSString *titleRaw = [NSString stringWithFormat:@"%@\n", @"itemForIndexPath.title"]; 
    NSAttributedString *title = [[NSAttributedString alloc] initWithString:titleRaw attributes:titleAttributes]; 
    [attrString appendAttributedString:title]; 

    UIFont *textFont = [UIFont systemFontOfSize:14]; 
    NSDictionary *textAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor clearColor], NSFontAttributeName: textFont};; 
    NSString *textRaw = itemForIndexPath.text; 
    if (![indexPath isEqual:self.currentSelection] && textRaw.length >= 30) 
    { 
     textRaw = [NSString stringWithFormat:@"%@...", [textRaw substringToIndex:27]]; 
    } 

    NSAttributedString *text = [[NSAttributedString alloc] initWithString:textRaw attributes:textAttributes]; 
    [attrString appendAttributedString:text]; 

    return attrString; 
} 

, 내가 수동으로 모든 텍스트 만 선택한 셀의 길이를 제한합니다.

그런 다음, cellForRowAtIndexPath에서,이 같은 할당 :

cell.textLabel.attributedText = [self attributedTextForIndexPath:indexPath]; 

heightForRowAtIndexPath에서을 : 나는 그것을 둘 것

NSAttributedString *text = [self attributedTextForIndexPath:indexPath]; 
CGSize constraintSize = CGSizeMake(255, CGFLOAT_MAX); 
CGSize size = [text boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size; 
return ceil(size.height + 22); // adding 22 for padding 
0

당신은 그것을 할 수있다 : -

-(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= @""; 
    return cell; 
} 
+0

를? 여전히'heightForRowAtIndexPath'가 필요합니까? – cLar

+0

아니요 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 = @ "" "; 반환 셀; } –

+0

빠른 응답을 보내 주셔서 감사합니다. 나는 그렇게하려고 시도했지만, 이제 선택 영역의 크기가 조정되지 않습니다. – cLar

0

당신은 자세한 내용은 텍스트의 높이 제한 (숨어 요소)를 추가하고 그 출구 수 있습니다. 그런 다음 해당 요소가 숨겨진 시점을 설정합니다. 제약 조건 == 0;

관련 문제