如何在 UITableViewCell 中获得正确的框架并且只初始化一次?
How to get correct frame and and only initialize once in UITableViewCell?
我正在 UITableViewCell 中以编程方式创建几个 UIButton,并在 layoutSubviews
中设置其框架。我这里也是设置选中状态。
但是,这似乎不是创建按钮状态或初始化按钮状态的最佳位置,因为 layoutSubviews
被多次调用。我可以在哪里放置以下代码,使框架正确,但只会初始化一次?
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 2.0, self.frame.size.height)];
button.selected = YES
我的一个想法是在 awakeFromNib
中初始化所有一次性设置,然后在 layoutSubviews
中进行实际的帧设置。这是一个好方法吗?
我建议将它放在
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
并使用自动布局放置按钮。我将向您展示如何使用 Masonry for Autolayout 来节省行数。
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIButton *button = [UIButton new];
button.selected = YES;
// Add button to self
[self addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top);
make.left.equalTo(self.mas_left);
make.width.mas_equalTo(self.view.frame.size.width/2);
make.height.equalTo(self.mas_height);
}
}
return self;
}
如果您将 NIB 文件用于您的自定义 UITableViewCell
class,那么 awakeFromNib
是您应该初始化 UIElement、class 属性和其他实例变量的方法你的 UITableViewCell
class.
否则,如果您使用的是自定义 UITableViewCell
class 而没有 NIB,那么您应该在
中初始化所有内容
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialize your frames here
}
return self;
}
现在,如果您想用不同的按钮状态或数据更新您的单元格,那么您只需从
- tableView:cellForRowAtIndexPath:
并根据需要为该特定单元格进行更改。
例如:
- tableView:(UITableView *)tableView cellForRowAtIndecPath:(NSIndexPath *)indexPath {
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
[cell someMethod];
return cell;
}
我正在 UITableViewCell 中以编程方式创建几个 UIButton,并在 layoutSubviews
中设置其框架。我这里也是设置选中状态。
但是,这似乎不是创建按钮状态或初始化按钮状态的最佳位置,因为 layoutSubviews
被多次调用。我可以在哪里放置以下代码,使框架正确,但只会初始化一次?
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 2.0, self.frame.size.height)];
button.selected = YES
我的一个想法是在 awakeFromNib
中初始化所有一次性设置,然后在 layoutSubviews
中进行实际的帧设置。这是一个好方法吗?
我建议将它放在
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
并使用自动布局放置按钮。我将向您展示如何使用 Masonry for Autolayout 来节省行数。
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIButton *button = [UIButton new];
button.selected = YES;
// Add button to self
[self addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top);
make.left.equalTo(self.mas_left);
make.width.mas_equalTo(self.view.frame.size.width/2);
make.height.equalTo(self.mas_height);
}
}
return self;
}
如果您将 NIB 文件用于您的自定义 UITableViewCell
class,那么 awakeFromNib
是您应该初始化 UIElement、class 属性和其他实例变量的方法你的 UITableViewCell
class.
否则,如果您使用的是自定义 UITableViewCell
class 而没有 NIB,那么您应该在
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialize your frames here
}
return self;
}
现在,如果您想用不同的按钮状态或数据更新您的单元格,那么您只需从
- tableView:cellForRowAtIndexPath:
并根据需要为该特定单元格进行更改。
例如:
- tableView:(UITableView *)tableView cellForRowAtIndecPath:(NSIndexPath *)indexPath {
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
[cell someMethod];
return cell;
}