dynamic单元高度自动布局IOS

我一直在按照本教程来实现dynamic单元格高度。 dynamic表视图单元格高度但它只适用于我的ios 7,而不是在ios 8中。但它在ipad上工作在ios 8,所以我有点难倒为什么它不能在iphone 。 虽然我完全遵循了代码,但教程和实现方式之间存在一些差异,因为我对自动布局和表格不够了解,所以我不确定这是否是导致我的问题的原因。

首先在教程中,tableView的尾部和前沿完全等于0到超级视图。 在我的实现中,我创build了比例约束,以便在多个不同的布局中缩放tableView。 进一步解释:

这是我的TableView使用多个不同设备的比例约束的图片。 在这里输入图像说明

为了实现这一点,我实施了以下限制: 在这里输入图像说明

在这里输入图像说明

在这里输入图像说明

在这里输入图像说明

是否有可能的细胞高度不能通过AutoLayout来计算,因为tableView有一个dynamic的高度?

在我的实现和教程之间可以看到的第二个区别是数据源。 我从调用SLQ3Lite数据库中获取数据,而本教程从XML源中获取数据。 我相信数据填充的单元格,因为当我看着我在IPAD上的实现,我可以看到这样的数据: 在这里输入图像说明

但在iPhone上,这是显示:表是可见的,但没有单元格被写入到表中。 在这里输入图像说明

当我使用debugging器时,我可以看到正在成功从数据库中检索到logging,但是它们没有被写入到表中。

这是很长的代码(对不起)

#import "favouritedViewController.h" #import "DBManager.h" #import "favouritedCell.h" @interface favouritedViewController () @property (strong, nonatomic) IBOutlet UITableView *tableView; @property (strong, nonatomic) NSArray *favourites; @property (nonatomic, strong) DBManager *dbManager; typedef void (^CompletionBlock)(); -(void)loadData; -(void)reloadDataWithCompletions:(CompletionBlock)completionBlock; @end @implementation favouritedViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"tomhaisdb.sql"]; self.tableView.delegate = self; self.tableView.dataSource = self; [self loadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)loadData { NSString *query = @"select * from favourites"; if (self.favourites != nil) { self.favourites = nil; } self.favourites = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]]; /* [self reloadDataWithCompletions:^{ self.tableView.backgroundColor = [UIColor colorWithRed:28.0f/255.0f green:30.0f/255.0f blue:35.0f/255.0f alpha:1]; }];*/ [self reloadTableViewContent]; } /*-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; }*/ -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.favourites.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ return [self basicCellAtIndexPath:indexPath]; } - (void)reloadTableViewContent { dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; }); } -(favouritedCell *)basicCellAtIndexPath:(NSIndexPath *)indexPath { favouritedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"favouriteCell" forIndexPath:indexPath]; [self configureBasicCell:cell atIndexPath:indexPath]; return cell; } -(void)configureBasicCell:(favouritedCell *)cell atIndexPath:(NSIndexPath *)indexPath{ NSInteger indexOfTomhaisText = [self.dbManager.arrColumnNames indexOfObject:@"tomhaisText"]; NSString *tomhaisText = [[self.favourites objectAtIndex:indexPath.row] objectAtIndex:indexOfTomhaisText]; [self setTomhaisForCell:cell item:tomhaisText]; [self setAnswerForCell:cell item:tomhaisText]; // change this later } -(void)setTomhaisForCell:(favouritedCell *)cell item:(NSString *)item{ [cell.favouriteText setText:item]; } -(void)setAnswerForCell:(favouritedCell *)cell item:(NSString *)item{ [cell.answer setText:item]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [self heightForFavouriteCellAtIndexPath:indexPath]; } -(CGFloat)heightForFavouriteCellAtIndexPath:(NSIndexPath *)indexPath{ static favouritedCell *sizingCell = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sizingCell = [self.tableView dequeueReusableCellWithIdentifier:@"favouriteCell"]; }); [self configureBasicCell:sizingCell atIndexPath:indexPath]; return [self calculateHeightForConfiguredSizingCell:sizingCell]; } -(CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell{ sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.frame), CGRectGetHeight(sizingCell.bounds)); [sizingCell setNeedsLayout]; [sizingCell layoutIfNeeded]; CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; return size.height + 1.0f; } -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 155.0f; } 

编辑1


在这里输入图像说明在这里输入图像说明在这里输入图像说明在这里输入图像说明在这里输入图像说明在这里输入图像说明

这是它在ios 7上运行的图片(这是工作) 在这里输入图像说明

这是日志输出

2015-06-01 18:43:40.855事实[62233:607]行数:2 2015-06-01 18:43:43.996事实[62233:607]布局{{0,0},{256, 82}} 2015-06-01 18:43:55.068事实[62233:607]布局之前的内容视图{{0,0},{256,82}} 2015-06-01 18:44:09.409事实[62233: 607]布局{{0,0},{256,82}}之后的界限2015-06-01 18:44:12.843事实[62233:607]布局之前的内容视图{{0,0},{256,82} } 2015-06-01 18:44:21.462事实[62233:607]布局{{0,0},{256,82}}之前的界限2015-06-01 18:44:23.884事实[62233:607]在布局之前查看{{0,0},{256,82}} 2015-06-01 18:44:30.536事实[62233:607]布局{{0,0} 06-01 18:44:32.278事实[62233:607]布局之前的内容视图{{0,0},{256,82}}

从这段代码:

  -(CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell{ sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.frame), CGRectGetHeight(sizingCell.bounds)); NSLog(@"Bounds before Layout %@", NSStringFromCGRect(sizingCell.bounds)); NSLog(@"Content View before Layout %@", NSStringFromCGRect(sizingCell.contentView.bounds)); [sizingCell setNeedsLayout]; [sizingCell layoutIfNeeded]; CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; NSLog(@"Bounds after layout %@", NSStringFromCGRect(sizingCell.bounds)); NSLog(@"Content View before Layout %@", NSStringFromCGRect(sizingCell.contentView.bounds)); return size.height + 1.0f; } 

但在ios8上,这是我所看到的: 在这里输入图像说明

这是输出:

2015-06-01 18:47:14.688事实[62306:81355636]布局之前的界限{{0,0},{256,44}} 2015-06-01 18:47:14.688事实[62306:81355636]内容视图布局{{0,0},{256,44}}之前2015-06-01 18:47:14.688事实[62306:81355636]布局{{0,0},{256,44}} 2015-06 -01 18:47:14.688 Facts [62306:81355636] Layout {{0,0},{256,44}}之前的内容视图}

ios7中的sizingCell正在读取单元格内容的正确边界,但是在ios8中却没有。

我发现https://github.com/forkingdog/UITableView-FDTemplateLayoutCell库有用。 我尝试了所有,但没有工作。 现在这个库dynamic地计算UITableView行的高度。

你做得太多了 苹果可以为你处理前后距。

0 正确的号码使用,但你钉在superview.margin ,而不是超superview 。 苹果会根据设备将保证金调整到build议的保证金水平。 (更宽4.7“,更窄4”)。

仅供参考,任何乘数的0倍始终为0,所以您当前的约束条件不会达到您所期望的。

单元格高度closures的原因是由于错误的框架(Interface Builder无法警告,因为没有为任何Xany安装标签约束)。

IOS 7仍然可以解决单元格高度,但iOS 8要求单元格高度初始正确 (可解决)。