2012-01-17 3 views
1

나는 얼마나 많은 텍스트가 셀에 있는지에 따라 동적으로 높이를 변경하려고 노력 중입니다. 현재는 줄 바꿈 단어가 있습니다. 그러나 셀 내용이 많은 경우 (if 그것은 세 번째 줄로 간다.) 두 번째 줄을 지나서 아무것도 볼 수 없다.동적 인 UITableViewCell 높이

이것은 내가 지금까지 가지고있는 것입니다. 누군가가 뭔가를 놓치고 있거나 뭔가 잘못하고있는 것을 누군가가 볼 수 있기를 바랍니다 ... 어떤 도움을 주시면 감사하겠습니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UILabel *label = nil; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     label = [[UILabel alloc] initWithFrame:CGRectZero]; 
     [label setLineBreakMode:UILineBreakModeWordWrap]; 
     [label setMinimumFontSize:FONT_SIZE]; 
     [label setNumberOfLines:0]; 
     [label setFont:[UIFont systemFontOfSize:FONT_SIZE]]; 
     [label setTag:1]; 

     [[cell contentView] addSubview:label]; 
    } 

    // //Display cells with data that has been sorted from startSortingTheArray 
NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]; 
NSString *key = [keys objectAtIndex:indexPath.row]; 

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

if (!label) 
    label = (UILabel*)[cell viewWithTag:1]; 

[label setText:key]; 
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))]; 






//Applise current key value to cell text label 
//cell.textLabel.text = key; 
return cell; 
} 

//Cell size for word wrapping. 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 

    //Display cells with data that has been sorted from startSortingTheArray 
    NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]; 
    NSString *key = [keys objectAtIndex:indexPath.row]; 

    NSLog(@"%@", key); 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = MAX(size.height, 44.0f); 

    return height + (CELL_CONTENT_MARGIN * 2); 
} 

업데이트 코드 위 어쨌든 나를 위해 지금 작업 버전 .. :)

+0

문제는 세포의 높이가 아니라 라벨의 높이와 같아 보입니다. 그 맞습니까? 레이블의 높이를 어디에서나 설정할 수 있기 때문에 의미가 있습니다. – sosborn

+0

hrmm ... 글쎄요. 아마도 ... 나는 내가 괜찮은 튜토리얼을 발견했다고 생각하는 라벨을 정리해 보겠습니다. 나는 지금 전에 그것을 줄 것이다 ... –

+0

나는 UILabel을 추가하고 셀의 크기를 약 두 배로 늘리십시오. 그러나 동적으로 확대하고자하는 텍스트가 많은 셀뿐만 아니라 tableview의 모든 셀에도 발생합니다. –

답변

3

이상 시작하고 단어에 대한이 튜토리얼 단어를 따라 당신이 잘 될 것입니다 : Cocoa is My Girlfriend

이 실제로 당신이이 튜토리얼을 보았을 때 그 부분 만 따라 간 것처럼 보입니다. 나는 아직도 당신이 라벨의 프레임을 설정하는 것을 보지 못한다.

+0

옙 내가 지금까지 해왔 던 그 중 하나가 그것과 관련이있다. ** cell = [[[UITableViewCell alloc] initWithFrame : CGRectZero reuseIdentifier : @ " Cell "] autorelease]; ** ARC initWithFrame을 사용하는 경우 autorelease 제외 : reuseIdentifier : 중단되었으므로 여기서 수행 할 작업을 확신 할 수 없습니다 ... –

+0

알았어 작동 중 ... 셀 선택이 엉망이되는 것 같습니다. 액세서리보기 지금은 아프지 만 그 문제를 직접 알아 내려고 .. 도와 줘서 고마워. –