自调整单元格对我不起作用
Self sizing cell just doesn't work for me
我试着按照 WWCD 2014 session 226,它介绍了使用自动布局在 iOS 8 中实现自调整单元格大小的方法,但它不能正常工作。
HHTableViewCell.h
#import <UIKit/UIKit.h>
@interface HHTableViewCell : UITableViewCell
@property (strong, nonatomic)UILabel *title;
@end
HHTableViewCell.m
#import "HHTableViewCell.h"
@implementation HHTableViewCell
@synthesize title = _title;
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// configure control(s)
#pragma mark -- title Lable
_title = [[UILabel alloc] initWithFrame:CGRectInset(self.bounds, 15.0, 0.0)];
_title.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
_title.numberOfLines = 0;
[self.contentView addSubview:_title];
#pragma mark -- constraints
NSMutableArray *constraints = [[NSMutableArray alloc]init];
UIView *contentView = self.contentView;
[constraints addObject:[NSLayoutConstraint
constraintWithItem:_title
attribute:NSLayoutAttributeFirstBaseline
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeTop
multiplier:1.8
constant:3.0]];
[constraints addObject:[NSLayoutConstraint
constraintWithItem:_title
attribute:NSLayoutAttributeFirstBaseline
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeTop
multiplier:1.8
constant:3.0]];
[constraints addObject:[NSLayoutConstraint
constraintWithItem:_title
attribute:NSLayoutAttributeFirstBaseline
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeTop
multiplier:1.8
constant:3.0]];
[constraints addObject:[NSLayoutConstraint
constraintWithItem:contentView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:0
multiplier:1.0
constant:44.0]];
[constraints addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-15-[_title]-15-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_title)]];
[self.contentView addConstraints:constraints];
}
return self;
}
@end
MMTableViewController.h
#import <UIKit/UIKit.h>
@interface MMTableViewController : UITableViewController
@end
MMTableViewController.m
#import "MMTableViewController.h"
#import "HHTableViewCell.h"
@interface MMTableViewController ()
@end
@implementation MMTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[HHTableViewCell class] forCellReuseIdentifier:@"HiCell"];
[self.tableView registerClass:[HHTableViewCell class] forCellReuseIdentifier:@"HCell"];
self.tableView.estimatedRowHeight = 44.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"HiCell";
HHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.title.text = @"Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. ";
// Configure the cell...
NSLog(cell.title.text);
return cell;
}
@end
单元格具有固定高度并包含两行文本。看起来像这样:
你好苹果。你好苹果。你好苹果。你好
阿普尔。你好苹果。你好苹果。你好苹果...
约束和子视图以编程方式添加。
模拟器是运行 iOS 8.3 in Xcode 6.3.1。
要使 UILabel 与约束一起工作,查看 Apple's documentation,我认为您需要设置 preferredMaxLayoutWidth
属性:
This property affects the size of the label when layout constraints
are applied to it. During layout, if the text extends beyond the width
specified by this property, the additional text is flowed to one or
more new lines, thereby increasing the height of the label.
但是,除非您想要一些特定的单元格自定义,否则您可以使用默认的 UITableViewCell,并在提供的 titleLabel
上设置 numberOfLines = 0
。它可以与 UITableViewAutomaticDimension
一起使用,尽管我只结合 heightForRowAtIndexPath:
.
对其进行了测试
更新:
据我了解,您还需要将 estimatedRowHeight
设置为 viewDidLoad
中的内容(该值似乎甚至不需要 accurate/important )。
Here is a working example 使用默认的 UITableViewCells:
我试着按照 WWCD 2014 session 226,它介绍了使用自动布局在 iOS 8 中实现自调整单元格大小的方法,但它不能正常工作。
HHTableViewCell.h
#import <UIKit/UIKit.h>
@interface HHTableViewCell : UITableViewCell
@property (strong, nonatomic)UILabel *title;
@end
HHTableViewCell.m
#import "HHTableViewCell.h"
@implementation HHTableViewCell
@synthesize title = _title;
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// configure control(s)
#pragma mark -- title Lable
_title = [[UILabel alloc] initWithFrame:CGRectInset(self.bounds, 15.0, 0.0)];
_title.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
_title.numberOfLines = 0;
[self.contentView addSubview:_title];
#pragma mark -- constraints
NSMutableArray *constraints = [[NSMutableArray alloc]init];
UIView *contentView = self.contentView;
[constraints addObject:[NSLayoutConstraint
constraintWithItem:_title
attribute:NSLayoutAttributeFirstBaseline
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeTop
multiplier:1.8
constant:3.0]];
[constraints addObject:[NSLayoutConstraint
constraintWithItem:_title
attribute:NSLayoutAttributeFirstBaseline
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeTop
multiplier:1.8
constant:3.0]];
[constraints addObject:[NSLayoutConstraint
constraintWithItem:_title
attribute:NSLayoutAttributeFirstBaseline
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeTop
multiplier:1.8
constant:3.0]];
[constraints addObject:[NSLayoutConstraint
constraintWithItem:contentView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:0
multiplier:1.0
constant:44.0]];
[constraints addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-15-[_title]-15-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_title)]];
[self.contentView addConstraints:constraints];
}
return self;
}
@end
MMTableViewController.h
#import <UIKit/UIKit.h>
@interface MMTableViewController : UITableViewController
@end
MMTableViewController.m
#import "MMTableViewController.h"
#import "HHTableViewCell.h"
@interface MMTableViewController ()
@end
@implementation MMTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[HHTableViewCell class] forCellReuseIdentifier:@"HiCell"];
[self.tableView registerClass:[HHTableViewCell class] forCellReuseIdentifier:@"HCell"];
self.tableView.estimatedRowHeight = 44.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"HiCell";
HHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.title.text = @"Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. Hello Apple. ";
// Configure the cell...
NSLog(cell.title.text);
return cell;
}
@end
单元格具有固定高度并包含两行文本。看起来像这样:
你好苹果。你好苹果。你好苹果。你好
阿普尔。你好苹果。你好苹果。你好苹果...
约束和子视图以编程方式添加。
模拟器是运行 iOS 8.3 in Xcode 6.3.1。
要使 UILabel 与约束一起工作,查看 Apple's documentation,我认为您需要设置 preferredMaxLayoutWidth
属性:
This property affects the size of the label when layout constraints are applied to it. During layout, if the text extends beyond the width specified by this property, the additional text is flowed to one or more new lines, thereby increasing the height of the label.
但是,除非您想要一些特定的单元格自定义,否则您可以使用默认的 UITableViewCell,并在提供的 titleLabel
上设置 numberOfLines = 0
。它可以与 UITableViewAutomaticDimension
一起使用,尽管我只结合 heightForRowAtIndexPath:
.
更新:
据我了解,您还需要将 estimatedRowHeight
设置为 viewDidLoad
中的内容(该值似乎甚至不需要 accurate/important )。
Here is a working example 使用默认的 UITableViewCells: