2016-12-26 125 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;