2012-08-23 5 views
2

내 UITableViewCell의 텍스트 라벨에있는 텍스트가 셀의 오른쪽에서 약 30 픽셀을 래핑하도록하려고합니다. 그러나 바로 오른쪽에서 약 5 또는 10 픽셀까지가는 중 .. 나는 cellForRowAtIndex에서 설정 한 모든 값을 편집 해 보았습니다. 그러나 변경된 사항은 셀의 높이이지만 셀의 textLabel 크기가 큰 것은 아닙니다. 이 오류를 발견 할 수 없으며 여기 누군가가 오류를 볼 수 있기를 희망합니다.UITableViewCell textLabel 너비가 변경되지 않습니다.

나는 더 많은 테스트를 해왔으며 내가 찾은 바는 sectionIndexTitlesForTableView을 사용하면 UItableViewCell textLabel이 아래의 코드에서 설명하는 올바른 크기로 줄어들 것입니다. 그러나이 방법을 비활성화하면 어떤 것도 지불하지 않습니다. 다음 코드에서 너비를 설정하려고 할 때주의하십시오.

이것은 다음과 같은 코드입니다.

//These Constants are used for dynamic cell height 
#define FONT_SIZE 18.0f 
#define CELL_CONTENT_WIDTH 290.0f 
#define CELL_CONTENT_MARGIN 10.0f 



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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    // Configure the cell using custom cell 

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

     [cell.textLabel setLineBreakMode:UILineBreakModeWordWrap]; 
     [cell.textLabel setMinimumFontSize:FONT_SIZE]; 
     [cell.textLabel setNumberOfLines:0]; 
     [cell.textLabel setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]]; 
     [cell.textLabel setTag:1]; 

     [[cell contentView] addSubview:cell.textLabel]; 
    } 

    //Customize cell here 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection 

    //Replaces previously selected cells accessory view (tick) 
    if ([indexPath isEqual:oldCheckedIndexPath]){ 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }else{ 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    //Display cells with data that has been sorted from startSortingTheArray 
    NSMutableArray *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]; 


    //Applise current key value to cell text label 
    [cell.textLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 25.0f))]; 

    [cell.textLabel setText:key]; 

    return cell; 
} 



//Cell size for word wrapping So the user can see all of the details of a submodel etc. 
- (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]; 

    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, 25.0f); 

    return height + (CELL_CONTENT_MARGIN * 2); 
} 

답변

10

셀의 layoutSubviews 방법을 사용하면 (또는 대부분의 다른 곳) cellForRow에서 수행하려고 무엇보다 우선합니다. 가장 좋은 해결책은 UITableViewCell을 서브 클래스 화하고 layoutSubviews를 오버라이드하는 것입니다. super를 호출 한 다음 텍스트 레이블 프레임을 사용자 정의하십시오.

편집 :

그냥이 (물론 수정) 구현 위

CustomTableViewCell *cell = [tableView dequeReusableCell... 

cell = [[CustomTableViewCell alloc] initWithStyle... 

대신 사용할 수 있습니다. 여분의 .xibs는 필요하지 않습니다.

@interface CustomTableViewCell : UITableViewCell 
    @end 

    @implementation CustomTableViewCell 
    - (void)layoutSubviews 
    { 
     [super layoutSubviews]; 
     cell.textLabel.frame = theFrameYouWant; 
    } 
    @end 
+1

dangit .. 'layoutSubviews'가 분실 되었습니까? – HurkNburkS

+0

UITableViewCell이 상속하는 UIView 메서드입니다. 이것은 여러 경우에 자동으로 호출되며 수동으로 호출 할 수 있습니다 (setNeedsLayout 및/또는 layoutIfNeeded를 적절하게 사용). 셀이 표시되기 전에 layoutSubview 메서드가 호출됩니다. 뷰 자체의 프레임/경계가 변경되거나 뷰 계층에 처음 들어가면 뷰의 하위 뷰 프레임을 구성하기위한 적절한 진입 점입니다. 자세한 내용은 다양한 문서를 참조하십시오. – Matt

+0

난 왜 'sectionIndexTitlesForTableView'가 활성화되어있을 때 작동하는지 이해하지 못한다. 그러나 그걸 사용하지 않으면 슬프고 도스틴 작업이 가능해진다. – HurkNburkS

관련 문제