gpt4 book ai didi

ios - 如何使用 AutoLayout 以编程方式创建自定义 UITableViewCell

转载 作者:IT王子 更新时间:2023-10-29 07:47:42 30 4
gpt4 key购买 nike

我正在尝试实现一个 UITableView,它的行为类似于 Twitter 客户端的时间线。现在我只是想在 UITableViewCell 中获取两个标签。正如this Stack Overflow answer推荐的,我为每个布局使用不同的 reuseIdentifier。我的布局很简单,由一个标签或两个标签组成。最终我将调整 UITableViewCells 的高度,但首先我需要让单元格填充内容。

如果我使用 initWithFrame: 设置它们的框架,我可以让标签显示出来,但是约束没有被实现。

  • 问题:是什么阻止了标签和约束的出现?我在 UITableViewCell 的实现中显然遗漏了一些东西,但我不知道它是什么。

  • 第二个问题:我是否为 viewDidLoad 中的每个 reuseIdentifier 正确注册了 UITableViewCell 类?

这可能会让人觉得很难,但 Interface Builder 让我感到困惑,我想用代码来完成这一切。

这是名为 TVTCell.h 的自定义 UITableViewCell 的代码:

static NSString * const kCellIDTitle = @"CellWithTitle";
static NSString * const kCellIDTitleMain = @"CellWithTitleMain";

@interface TVTCell : UITableViewCell
{
NSString *reuseID;
}

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *mainLabel;

@end

和 TVTCell.m:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
reuseID = reuseIdentifier;

nameLabel = [[UILabel alloc] init];
[nameLabel setTextColor:[UIColor blackColor]];
[nameLabel setBackgroundColor:[UIColor colorWithHue:32 saturation:100 brightness:63 alpha:1]];
[nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
[nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:nameLabel];

mainLabel = [[UILabel alloc] init];
[mainLabel setTextColor:[UIColor blackColor]];
[mainLabel setBackgroundColor:[UIColor colorWithHue:66 saturation:100 brightness:63 alpha:1]];
[mainLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
[mainLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:mainLabel];

[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];

}
return self;
}


- (void)updateConstraints
{
[super updateConstraints];

NSDictionary *views = NSDictionaryOfVariableBindings(nameLabel, mainLabel);
if (reuseID == kCellIDTitle) {
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel]|"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
}
if (reuseID == kCellIDTitleMain) {
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];
[self.contentView addConstraints:constraints];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainLabel]|"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];
[self.contentView addConstraints:constraints];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel][mainLabel]|"
options: NSLayoutFormatAlignAllLeft
metrics:nil
views:views];
[self.contentView addConstraints:constraints];

[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:nameLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:44.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:nameLabel
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:1]];
}
}

抱歉,大量代码。这是我的 UITableView 的 tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 3) {
TVTCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIDTitle forIndexPath:indexPath];

[[cell nameLabel] setText:[nameArray objectAtIndex:indexPath.row]];

return cell;
} else if (indexPath.row == 1 || indexPath.row == 4 || indexPath.row == 5) {
TVTCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIDTitleMain forIndexPath:indexPath];

[[cell nameLabel] setText:[nameArray objectAtIndex:indexPath.row]];
[[cell mainLabel] setText:[dataArray objectAtIndex:indexPath.row]];

return cell;
} else
{
UITableViewCell *badCell = [[UITableViewCell alloc] init];
NSLog(@"Warning! returning a cell that shouldnt be here");
badCell.textLabel.text = @"Warning!";
return badCell;
}
}

最后,UITableView 的 viewDidLoad 方法:

- (void)viewDidLoad
{
[super viewDidLoad];

[[self tableView] registerClass:[TVTCell class] forCellReuseIdentifier:kCellIDTitle];
[[self tableView] registerClass:[TVTCell class] forCellReuseIdentifier:kCellIDTitleMain];
}

最佳答案

您的代码有几处错误。首先,我想您会发现,如果您进行一些日志记录,永远不会调用 updateConstraints。我会将所有代码放在 init 方法中。此外,您的约束中有几处错误。不需要将高度设置为 44 的约束,因为您已经将标签固定到单元格的顶部和底部。我不知道你想用最后一个做什么,它看起来会使 nameLabel 1 点宽。此外,您不应将内容 View 的 translatesAutoresizingMaskIntoConstraints 设置为 NO,这会导致奇怪的效果。所以这是我认为你想要的代码:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
reuseID = reuseIdentifier;

nameLabel = [[UILabel alloc] init];
[nameLabel setTextColor:[UIColor blackColor]];
[nameLabel setBackgroundColor:[UIColor colorWithHue:32 saturation:100 brightness:63 alpha:1]];
[nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
[nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:nameLabel];

mainLabel = [[UILabel alloc] init];
[mainLabel setTextColor:[UIColor blackColor]];
[mainLabel setBackgroundColor:[UIColor colorWithHue:66 saturation:100 brightness:63 alpha:1]];
[mainLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
[mainLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:mainLabel];

NSDictionary *views = NSDictionaryOfVariableBindings(nameLabel, mainLabel);
if (reuseID == kCellIDTitle) {
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
options: 0
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel]|"
options: 0
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
}
if (reuseID == kCellIDTitleMain) {
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
options:0
metrics:nil
views:views];
[self.contentView addConstraints:constraints];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainLabel]|"
options: 0
metrics:nil
views:views];
[self.contentView addConstraints:constraints];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel][mainLabel(==nameLabel)]|"
options: 0
metrics:nil
views:views];
[self.contentView addConstraints:constraints];

}
}
return self;
}

关于ios - 如何使用 AutoLayout 以编程方式创建自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18969355/

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