我爲我的應用程序創建一個設置視圖,並在該視圖中是一個UITableView。我正在創建自定義單元格來滿足我的需求,但是我遇到了問題 - 只有最後一個單元格獲得[layoutSubviews]
。難道我做錯了什麼?自定義單元格,只有最後一個單元格獲取[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應該叫。有什麼建議麼?
謝謝你的詳細解答。但即使使用[self setNeedsLayout],而不是直接調用layoutSubviews,只有最後一行在旋轉視圖時纔會獲取layoutSubviews。 – mattsven 2013-05-07 17:44:17
我應該更仔細地閱讀你的代碼。 (佈局子視圖錯誤超過了我的注意力,這些應該肯定會被刪除)。我編輯了答案。首先獲得正確構建的單元格,佈局應該可以正常工作。如果沒有,在旋轉時嘗試tableView reloadData,但你不應該需要它。 – danh 2013-05-07 19:37:11
我沒有使用可重複使用的單元格,因爲這將是一個設置視圖,並且它是少量的單元格。不管如何,它似乎都不起作用。 – mattsven 2013-05-07 22:12:45