gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:23:42 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com