UILabel动态高度不起作用

时间:2011-06-11 11:56:02

标签: iphone objective-c cocoa-touch uilabel

我遇到麻烦使我的UILabel高度动态。标签包含在自定义TableViewCell

我设法使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;
}

1 个答案:

答案 0 :(得分: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)];