Ios tableview根据内容高度动态查看高度行
我的tableview行没有拉伸以适应ImageView,ImageView重叠。 我将tableview设置为自动标注 并将imageview的底部约束到单元格 肯定少了什么,但我想不出是什么 tableviewcellIos tableview根据内容高度动态查看高度行,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,我的tableview行没有拉伸以适应ImageView,ImageView重叠。 我将tableview设置为自动标注 并将imageview的底部约束到单元格 肯定少了什么,但我想不出是什么 tableviewcell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self.photoImageView = [[UI
- (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;
我需要将contentView的底部锚点约束到imageView,而不是相反
现在唯一的问题是我的tableview最初加载的单元格非常小
然后当我刷新图像时,图像变得更大
在tableview代理(tableviewcontroller)中,可以实现名为heightForrowAtIndexPath的函数
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
假设有一个包含所有图像的数组,则返回该索引中图像的相应高度 我觉得
setConstraints
中的这一行可疑:
[self.photoImageView.heightAnchor constraintEqualToConstant:65].active = YES;
如果使用的是自调整大小的表视图单元格,则可能不需要恒定的高度
这是一个关于自调整表视图单元格大小的非常好的教程,可能会有所帮助:只需在viewdidload()方法中设置表视图属性 ** ** 将tableview委托设置为
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
还可以在UITableView单元中设置元素,并从上到下附着约束
有关更多信息,请参阅此链接-我的问题完全与自动布局有关 我首先需要将单元格底部约束到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;
为什么不在故事板中创建一个可重用的单元,并在其中设置所有约束条件?可能重复的“哦,我没有实现heightforRow”。也不知道您可以在那里返回UITableviewAutomaticDimension。你要试试吗
[self.photoImageView.heightAnchor constraintEqualToConstant:65].active = YES;
[self.photoImageView.widthAnchor constraintEqualToConstant:65].active = YES;