2011-06-11 68 views
1

即時通訊有麻煩,使我的UILabel高度動態。該標籤包含在一個自定義TableViewCell中UILabel動態高度不起作用

我設法使TableViewCellHeight動態,但我不能使標籤多行。

每當我設置行數= 0(在IB以及代碼中),標籤/文本完全消失。 lineBreakMode也沒有做太多?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row]; 

    NSString *text = eventUp.date; 

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

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

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

    return height + (CELL_CONTENT_MARGIN * 2); 

    //return 66;  

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [appDelegate.eventDataUp count]; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"EventUpCVCell"; 

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

     self.eventUpCVCell = nil; 
     [[NSBundle mainBundle] loadNibNamed:@"EventUpCVCell" owner:self options:nil]; 
     cell = self.eventUpCVCell; 

     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]; 
     } 
    Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row]; 

    UILabel *lblMonth = (UILabel *) [cell viewWithTag:kLabel1Tag]; 
    UILabel *lblTitle = (UILabel *) [cell viewWithTag:kLabel2Tag]; 
    UILabel *lblDate = (UILabel *) [cell viewWithTag:kLabel3Tag]; 

    //month.text = eventUp.month; 
    lblTitle.text = eventUp.title; 
    lblDate.text = eventUp.date; 

    //lblDate.lineBreakMode = UILineBreakModeWordWrap; 
    lblDate.numberOfLines = 1; 

    lblMonth.text = [NSString stringWithFormat:@"%@ ", eventUp.month]; 



    //cell.textLabel.text = eventUp.title; 

    //cell.detailTextLabel.text = eventUp.date; 

    //NSLog(@"Hello"); 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    // Set up the cell 
    return cell; 
} 
+0

要哪個標籤你正試圖在這裏建立動態的高度。 – EmptyStack 2011-06-11 13:01:05

回答

3

你必須創建後設置標籤框架,其實你用CGRectZero幀創建

嘗試:

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

     Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row]; 

     NSString *text = eventUp.date; 

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

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

     [label setFrame:CGRectMake(0,0,size.width,size.height)];