gpt4 book ai didi

iphone - 自定义单元格,只有最后一个单元格获得 [layoutSubviews]?

转载 作者:行者123 更新时间:2023-11-28 20:19:54 27 4
gpt4 key购买 nike

我正在为我的应用程序创建一个设置 View ,该 View 中有一个 UITableView。我正在创建自定义单元格以满足我的需要,但我遇到了问题 - 只有最后一个单元格正在获取 [layoutSubviews]。我做错了什么吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//int type = (indexPath.row == 0?1:0);
//if(indexPath.row == 6) type = 2;

NSLog(@"row %i created", indexPath.row);
TableCell *cell = [[TableCell alloc] initWithType:indexPath.row];

cell.textLabel.text = @"Test cell";

return cell;
}

在我的自定义单元格中:

@implementation TableCell

UIImageView *shadowView;
int row;

- (id) initWithType:(int)type {
row = type;

self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

self.backgroundColor = [UIColor clearColor];
self.backgroundView = [[UIView alloc] init];

UIImage *shadowImage = [UIImage imageNamed:@"CellShadow"];
shadowImage = [shadowImage resizableImageWithCapInsets:UIEdgeInsetsMake(14, 14, 14, 14)];

shadowView = [[UIImageView alloc] initWithImage:shadowImage];

[self.contentView addSubview:shadowView];
//[self.contentView sendSubviewToBack:shadowView];

NSLog(@"agreed, row %i created", row);

[self layoutSubviews];

return self;
}

- (void) layoutSubviews {
NSLog(@"row: %i", row);

[super layoutSubviews];

shadowView.frame = CGRectMake(
0, 0,
self.contentView.frame.size.width,
self.contentView.frame.size.height
);
}

@end

连续地,只有最后一个单元格 #6 在我旋转时或在应该调用 layoutSubviews 时被报告。有什么建议吗?

最佳答案

不要直接调用layoutSubviews。使用 [self setNeedsLayout][self layoutIfNeeded]。但是根本不要在单元格的 init 方法中调用它们。

此外,不要调用 [[TableCell alloc] initWithType:indexPath.row];直接,要么。相反,使用...

- (id)dequeueReusableCellWithIdentifier:(NSString *)IndexPath 标识符:(NSIndexPath *)indexPath

构建该单元格后,您可以告诉它它是行,但请注意,随着表格的滚动,单元格会被回收,因此您必须在每次调用 cellForRowAtIndexPath 时更新该值。

当表格 View 调整大小时,单元格应该重新布局(无需直接或间接调用)。

参见 tableview doc here .

关于iphone - 自定义单元格,只有最后一个单元格获得 [layoutSubviews]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16424871/

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