iOS - 表格视图单元格,UITextView未正确调整大小

时间:2013-02-18 21:02:31

标签: ios textview height tableview cell

我已经将一个单元类子类化,并且正在使用Storyboard中的原型。有问题的单元格有一个嵌入式文本视图,可以显示不同长度的文本。 我已经在 heightForRowAtIndexPath:中设置了单元格高度,但是尝试在 cellForRowAtIndexPath:中调整textview本身的大小无法正常工作。 它不会在初始加载时调整大小,但是一旦我将单元格滚出视图然后重新进入。但是如果我继续来回滚动,它偶尔会再次收缩。

代码:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int defaultSize = 0;
    int height = 0;
    if(indexPath.section%2==0){
        defaultSize = 160;
        NSMutableDictionary *currentPost = [newsFeedPosts objectAtIndex:indexPath.section];
        NSString *string = [currentPost valueForKey:@"detailsText"];
        CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
        height = stringSize.height + defaultSize;
        NSLog(@"String Size Before: %f",stringSize.height);

    }else{
        defaultSize = 270;
        height = defaultSize;
    }
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;
    if(indexPath.section%2==0){
        CellIdentifier = @"NewsFeedText";
    }else{
        CellIdentifier = @"NewsFeedImage";
    }
    NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    [cell setBackgroundView:[[NewsFeedCellBackground alloc] init]];
    [cell.bottomContainer setClipsToBounds:YES];
    [cell.bottomContainer.layer setMasksToBounds:YES];
    [cell.imagePreview.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [cell.bottomContainer setFrame:CGRectMake(0, cell.contentView.frame.size.height-30, cell.contentView.frame.size.width, 30)];

    //Data Object
    NSMutableDictionary *currentPost = [newsFeedPosts objectAtIndex:indexPath.section];
    //View
    cell.userImage.image = [UIImage imageNamed:[currentPost valueForKey:@"userImage"]];
    cell.userName.text = [currentPost valueForKey:@"userName"];
    cell.createdDate.text = [currentPost valueForKey:@"createdDate"];
    cell.patientName.text = [currentPost valueForKey:@"patientName"];
    cell.detailsText.text = [currentPost valueForKey:@"detailsText"];
    [cell.imagePreview setImage:[UIImage imageNamed:[currentPost valueForKey:@"imagePreview"]] forState:UIControlStateNormal];
    [cell.viewCase addTarget:self action:@selector(displayCase:) forControlEvents:UIControlEventTouchUpInside];

    //Image
    [cell.imagePreview addTarget:self action:@selector(displayImage:) forControlEvents:UIControlEventTouchUpInside];

    //Data
    cell.viewCase.tag = [[currentPost valueForKey:@"caseID"] intValue];

    //Cell Adjustments
    NSString *string = [currentPost valueForKey:@"detailsText"];
    CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"String Size After: %f",stringSize.height);
    [cell.detailsText setFrame:CGRectMake(cell.detailsText.frame.origin.x, cell.detailsText.frame.origin.y, cell.detailsText.frame.size.width, stringSize.height+10)];
    [cell.detailsText setBackgroundColor:[UIColor redColor]];

    return cell;
}

1 个答案:

答案 0 :(得分:3)

使用自动布局结果允许我将顶部空间和底部空间设置为superview(单元格视图),并且由于 heightForRowAtIndexPath:中的单元格高度已更改,因此textview会自动调整大小