2016-12-26 127 views
1

我的tableview行不适合图像浏览和图像浏览重叠。 enter image description here根据内容高度的tableview动态高度行

我设置自动尺寸 的tableview中和约束ImageView的底部到小区

一定有什么缺失,但我无法弄清楚什么。

tableviewcell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self.photoImageView = [[UIImageView alloc]init]; 
    if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) 
     return nil; 

    [self.contentView addSubview:self.photoImageView]; 

    [self setConstraints]; 

    return self; 
} 

-(void) layoutSubviews { 
    [super layoutSubviews]; 

    self.nameLabel.preferredMaxLayoutWidth = self.nameLabel.frame.size.width; 

    [self setConstraints]; 

} 

- (void) setConstraints { 

    UILayoutGuide *margins = self.contentView.layoutMarginsGuide; 

    self.photoImageView.translatesAutoresizingMaskIntoConstraints = false; 
    [self.photoImageView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:7].active = YES; 
    [self.photoImageView.bottomAnchor constraintGreaterThanOrEqualToAnchor:self.contentView.bottomAnchor constant:12].active = YES; 
    [self.photoImageView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:8].active = YES; 
    [self.photoImageView.heightAnchor constraintEqualToConstant:65].active = YES; 
    [self.photoImageView.widthAnchor constraintEqualToConstant:65].active = YES; 
    self.photoImageView.contentMode = UIViewContentModeScaleAspectFit; 
    self.photoImageView.layer.cornerRadius = self.photoImageView.frame.size.height/3; 
    self.photoImageView.layer.masksToBounds = YES; 

} 

tableviewcontroller

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    //tableview 
    NSString *cellIdentifier = @"cell"; 
    [self.businessesTableView registerClass:[YPBusinessTableViewCell class] forCellReuseIdentifier:cellIdentifier]; 
    self.businessesTableView.delegate = self; 
    self.businessesTableView.dataSource = self; 
    self.refreshControl = [[UIRefreshControl alloc]init]; 
    [self.businessesTableView addSubview:self.refreshControl]; 
    [self.refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged]; 

    UIEdgeInsets insets = self.businessesTableView.contentInset; 
    insets.bottom += YPInfiniteScrollActivityView.defaultHeight; 
    self.businessesTableView.contentInset = insets; 

    self.businessesTableView.estimatedRowHeight = 120; 
    self.businessesTableView.rowHeight = UITableViewAutomaticDimension; 

    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 

    UIBarButtonItem *filter = [[UIBarButtonItem alloc] initWithTitle:@"Filter" style:UIBarButtonItemStylePlain target:self action:@selector(presentFilterView)]; 
    negativeSpacer.width = -14; 

    [self.navigationItem setLeftBarButtonItems:@[negativeSpacer, filter] animated:NO]; 


    [self setConstraints]; 
    [self doSearch:@"test"]; 

    [self setupInfiniteScrollView]; 
    [self addSearchBar]; 
    [self hideErrorView:self.errorView]; 


} 

编辑:

我想通了,我的问题是改变这一行

[self.contentView.bottomAnchor constraintGreaterThanOrEqualToAnchor:self.photoImageView.bottomAnchor constant:0].active = YES; 

我需要约束的B将contentView锚定到imageView而不是其他方式。

唯一的问题是我现在的tableview负载最初非常小的小区 然后当我刷新图像变得更大enter image description here

enter image description here

+1

为什么你不只是在storyBoard中创建一个可重用的单元格,并设置所有的约束呢? –

+0

[动态更改UITableView高度]的可能重复(http://stackoverflow.com/questions/14223931/change-uitableview-height-dynamically) –

回答

0

在你的tableview委托(tableviewcontroller),你可以实现一个功能所谓heightForrowAtIndexPath

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

假设你有所有的图像阵列,在该索引处返回到图像的相应的高度。

0

这条线setConstraints看起来可疑对我说:

[self.photoImageView.heightAnchor constraintEqualToConstant:65].active = YES; 

如果您使用的自调整大小表视图细胞,你可能不希望一个恒定的高度。

这是自调整大小表视图细胞相当不错的教程,这可能是有帮助的:https://www.raywenderlich.com/129059/self-sizing-table-view-cells

1

在viewDidLoad中只需设置的tableview属性()方法

**

tableView.estimatedRowHeight = 2000; // example 
tableView.rowHeight = UITableViewAutomaticDimension; 

**

设置桌面代表为

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: 
(NSIndexPath *)indexPath{ 
return UITableViewAutomaticDimension; 
} 

而且设置的UITableView细胞中的元素从顶部连接约束下

请参阅此链接了解更多信息 - 以自动版式https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html

+0

哦,我没有实现heightforRow。也不知道你可以在那里返回UITableviewAutomaticDimension。去尝试一下 – mparrish91

0

我的问题完全相关。

我首先需要将单元格的底部限制在imageviews底部,而不是其他方式。

[self.contentView.bottomAnchor constraintGreaterThanOrEqualToAnchor:self.photoImageView.bottomAnchor constant:0].active = YES; 

之后我在重新载入表格后遇到了imageview的启动大小和大小问题。

然后,我在imageview上设置固定宽度和高度,并解决了所有其他尺寸/重新加载问题。

[self.photoImageView.heightAnchor constraintEqualToConstant:65].active = YES; 
[self.photoImageView.widthAnchor constraintEqualToConstant:65].active = YES;