自定义单元格调整问题

时间:2015-12-15 13:01:05

标签: ios objective-c uitableview custom-cell

当数据加载时,它显示完美的约束,并且数据以完美的间距完美显示......

表格加载时请查看附件:

enter image description here

当我们拥有较少的标题时,它会显示间距:

enter image description here

我正在从文件中加载数据:

代码如下:

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

    Customviewcell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    UIImage * image = [UIImage imageNamed:justThumbs[indexPath.row]];
    cell.Customimage.image=image;

    cell.CustomTitle.text=justTitles[indexPath.row];
    [cell.CustomTitle sizeToFit];


    NSString * htmlString =justDesc[indexPath.row];
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    cell.CustomDes.attributedText=attrStr;

    [cell.CustomDes sizeToFit];
    [cell setNeedsDisplay];
    [cell layoutIfNeeded];
    return cell;
}

从文件中加载数据如下:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [tableviewcustom reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL * url = [[NSBundle mainBundle]URLForResource:@"services" withExtension:@"plist"];
    Servicestitle = [NSDictionary dictionaryWithContentsOfURL:url];
    justTitles = [Servicestitle objectForKey:@"Title"];
    justThumbs= [Servicestitle objectForKey:@"Thumbnail"];
    justDesc=[Servicestitle objectForKey:@"Description"];

    tableviewcustom.estimatedRowHeight = 150;
    tableviewcustom.rowHeight = UITableViewAutomaticDimension;
    [tableviewcustom reloadData];


    // Do any additional setup after loading the view, typically from a nib.
}

故事板如下:

enter image description here

CustomDes Constrain: enter image description here

为title和desc添加背景:

enter image description here

前3个细胞:

enter image description here

在前3个细胞之后:

enter image description here

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您可能希望使用此TableView委托的方法:

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

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath: