动力单元格高度自动布局IOS [英] Dynamic Cell Height autolayout IOS
问题描述
我一直在关注这个教程实现动态单元格高度。 动态表视图细胞高度一>但它只是在我的ios 7并没有在ios一起8.但是它在iPad上的iOS 8的工作,所以我有点难倒为什么它不会对iphone工作。虽然我也跟着code pretty正好有一对夫妇的教程,我已经实现了它,当我不知道有足够的了解和自动版式表的方式之间的差异,我不知道这是是什么原因造成我的问题。
I have been following this tutorial to implement dynamic cell height. dynamic-table-view-cell-height But it only works in ios 7 for me and not in ios 8. However it does work in ios 8 on the iPAD so I'm a little stumped why it doesn't work on the iphone. Whilst I have followed the code pretty exactly there are a couple of differences between the tutorial and the way I have implemented it and as I don't know enough about autolayout and tables I'm not sure if this is what is causing my problem.
首先对教程的tableView已经缘和前缘正好等于0到上海华。在我的实现,使我多不同的布局中缩放的tableView我创建的比例限制。为了进一步说明:
Firstly on the tutorial the tableView has trailing and leading edges exactly equal to 0 to the superview. In my implementation I have created proportional constraints so that I scale the tableView within multiple different layouts. To explain further:
下面是一个使用多个不同的设备比例约束我的TableView的照片。
Here is a picture of my TableView using proportional constraints across multiple different devices.
要实现这一目标,我实现了以下限制:
To achieve this I implemented the following constraints:
是否有可能使细胞高度不能自动版式来计算,因为的tableView有一个动态的高度?
Is it possible that the cell height cannot be calculated by AutoLayout because the tableView has a dynamic height?
这是我可以把实现和教程之间看到的第二个区别是数据源。我从一个电话把我的数据到数据库SLQ3Lite,而教程从XML提要利用其数据。我相信,数据填充单元格,因为当我看着我在iPad上实现,我可以看到像这样的数据:
The second difference that I can see between my implementation and the tutorial is the data source. I am taking my data from a call to an SLQ3Lite database whereas the tutorial is taking its data from an xml feed. I am confident that the data is populating the cells because when I look at my implementation on an IPAD I can see the data like so:
但在iPhone这就是出现:
该表是可见的,但没有细胞已被写入表。
But on an iphone this is what appears: The table is visible but no cells have been written to the table.
当我使用调试器我可以看到,记录被成功地从数据库中检索,但它们不被写入表。
When I use the debugger I can see that records are being successfully retrieved from the database, but they are not being written to the table.
下面是code这是很长的(对不起)
Here is the code which is very long (Sorry)
#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上运行它的照片(这是工作)
Here is a picture of it running on ios 7 (which is working)
这是日志输出
2015年6月1日18:43:40.855事实[62233:607]行数:2 2015年6月1日
18:43:43.996事实[62233:607]边界布局之前{{0,0},{256,82}}
2015年6月1日18:43:55.068事实[62233:607]前布局内容的浏览
{{0,0},{256,82}} 2015年6月1日18:44:09.409事实[62233:607]的界
布局后{{0,0},{256,82}} 2015年6月1日18:44:12.843
事实[62233:607]前布局内容的浏览{{0,0},{256,82}}
2015年6月1日18:44:21.462事实[62233:607]边界布局{{0之前,0},
{256,82}} 2015年6月1日18:44:23.884事实[62233:607]内容查看
布局之前{{0,0},{256,82}} 2015年6月1日18:44:30.536
事实[62233:607]边界布局后{{0,0},{256,82}} 2015年6月1日
18:44:32.278事实[62233:607]内容视图布局{{0之前,0},
{256,82}}
2015-06-01 18:43:40.855 Facts[62233:607] Number of rows: 2 2015-06-01 18:43:43.996 Facts[62233:607] Bounds before Layout {{0, 0}, {256, 82}} 2015-06-01 18:43:55.068 Facts[62233:607] Content View before Layout {{0, 0}, {256, 82}} 2015-06-01 18:44:09.409 Facts[62233:607] Bounds after layout {{0, 0}, {256, 82}} 2015-06-01 18:44:12.843 Facts[62233:607] Content View before Layout {{0, 0}, {256, 82}} 2015-06-01 18:44:21.462 Facts[62233:607] Bounds before Layout {{0, 0}, {256, 82}} 2015-06-01 18:44:23.884 Facts[62233:607] Content View before Layout {{0, 0}, {256, 82}} 2015-06-01 18:44:30.536 Facts[62233:607] Bounds after layout {{0, 0}, {256, 82}} 2015-06-01 18:44:32.278 Facts[62233:607] Content View before Layout {{0, 0}, {256, 82}}
从这块code的:
-(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上,这是我所看到的:
But on ios8 this is what I see:
和此是输出:
2015年6月1日18:47:14.688事实[62306:81355636]边界布局前
{{0,0},{256,44}} 2015年6月1日18:47:14.688事实[62306:81355636]
布局之前内容的浏览{{0,0},{256,44}} 2015年6月1日18:47:14.688
事实[62306:81355636]边界布局后{{0,0},{256,44}}
2015年6月1日18:47:14.688事实[62306:81355636]之前内容的浏览
布局{{0,0},{256,44}}
2015-06-01 18:47:14.688 Facts[62306:81355636] Bounds before Layout {{0, 0}, {256, 44}} 2015-06-01 18:47:14.688 Facts[62306:81355636] Content View before Layout {{0, 0}, {256, 44}} 2015-06-01 18:47:14.688 Facts[62306:81355636] Bounds after layout {{0, 0}, {256, 44}} 2015-06-01 18:47:14.688 Facts[62306:81355636] Content View before Layout {{0, 0}, {256, 44}}
该sizingCell在ios7被阅读单元格的内容正确的界限,但在iOS8上事实并非如此。
The sizingCell in ios7 is reading the correct bounds for the cell's contents but in ios8 it is not.
推荐答案
我发现 https://开头github上。 COM / forkingdog / UITableView的-FDTemplateLayoutCell 的库非常有用。我已经尝试了所有,但不起作用。现在,这个库动态计算的UITableView行的高度。
I found https://github.com/forkingdog/UITableView-FDTemplateLayoutCell library useful. I have tried all but not work. Now this library dynamically calculate height of UITableView row.
这篇关于动力单元格高度自动布局IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!