iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS – 表格视图单元格,UITextView未正确调整大小大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经将一个元类子类化,并且正在使用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.bottomcatontainer setClipsToBounds:YES];
    [cell.bottomcatontainer.layer setMasksToBounds:YES];
    [cell.imagePreview.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [cell.bottomcatontainer 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;
}

解决方法

事实证明,使用自动布局允许我将Top空间和Bottom空间设置为superview(单元格视图),并且由于heightForRowATindexPath中的单元格高度已更改:textview随后会自动调整大小.

大佬总结

以上是大佬教程为你收集整理的iOS – 表格视图单元格,UITextView未正确调整大小全部内容,希望文章能够帮你解决iOS – 表格视图单元格,UITextView未正确调整大小所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。