ios - 自调整单元格对我不起作用

标签 ios objective-c uitableview ios-autolayout

我尝试关注 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


单元格具有固定高度并包含两行文本。看起来像这样:

你好苹果。你好苹果。你好苹果。你好
苹果。你好苹果。你好苹果。你好苹果...


约束和 subview 以编程方式添加。 模拟器在 Xcode 6.3.1 中运行 iOS 8.3。

最佳答案

要使 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 中的内容(该值似乎不需要准确/重要) ).

Here is a working example使用默认的 UITableViewCells:

关于ios - 自调整单元格对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869807/

相关文章:

ios - 具有相关 Objective-C Pod 的 Swift Pod 的桥接头?

ios - Xcode - 如何修复 'NSUnknownKeyException' ,原因 : … this class is not key value coding-compliant for the key X"error?

objective-c - NSMutableDictionary 的字面创建是否比类辅助方法效率低?

ios - @property definitions with ARC : strong is the default value, 但基本类型的默认值是多少?

ios - 如何检测 TableView 中的部分不在 View 中?

iphone - 图像未使用 UIImage drawInRect 缩放? (见附件代码)

ios - 设备重启时的 LocalNotification

ios - Firebase 动态链接 : How to get the URI scheme from the dynamic link that gets generated?

ios - 如何淡出表格选择

ios - NSMutableArray 和指针 : an operation on the pointer is an operation on the original array (? )