use*_*477 1 objective-c uitableview
我在向我添加按钮时遇到问题UITableViewCell
,单元格有两个UILabel
s和两个UIImageView
s,有时UIImageView
会包含一个图像,有时会包含一个按钮:
在我的UITableViewCell
子类中,我有:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if ( self ) {
// Initialization code
firstLock = [[UILabel alloc]init];
[firstLock setBackgroundColor:[UIColor clearColor]];
firstLock.textAlignment = UITextAlignmentLeft;
firstLock.font = [UIFont fontWithName:@"Arial-BoldMT" size:17];
secondLock= [[UILabel alloc]init];
[secondLock setBackgroundColor:[UIColor clearColor]];
secondLock.textAlignment = UITextAlignmentRight;
secondLock.font = [UIFont fontWithName:@"Arial-BoldMT" size:17];
firstLockImage = [[UIImageView alloc]init];
secondLockImage = [[UIImageView alloc] init];
[self.contentView addSubview:firstLock];
[self.contentView addSubview:secondLock];
[self.contentView addSubview:firstLockImage];
[self.contentView addSubview:secondLockImage];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
当其中一个UIImageView
s只是一个图像没问题,但是当我添加一个UIButton
(成像)作为子视图时它会崩溃.
在UITableViewDataSource
实施中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *btnImage = [UIImage imageNamed:@"bike_ok.png"];
UIButton *button =[UIButton alloc];
[button setImage:btnImage forState:UIControlStateNormal];
[cell.secondLockImage addSubview:button];
Run Code Online (Sandbox Code Playgroud)
添加按钮作为图像视图的子视图会崩溃:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Requesting the window of a view (<UIButton: 0x7bac7d0; frame = (0 0; 0 0); transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = (null)>) with a nil layer. This view probably hasn't received initWithFrame: or initWithCoder:.'
*** First throw call stack:
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
谢谢!
加上吧!这条线非常重要
[firstLockImage setUserInteractionEnabled:YES];
[secondLockImage setUserInteractionEnabled:YES];
Run Code Online (Sandbox Code Playgroud)
因为UIImageView默认为NO,没有它就没有按钮!
如果您阅读了崩溃错误,则很容易看出您的问题所在.您尚未初始化按钮.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,0,0)];
Run Code Online (Sandbox Code Playgroud)
或者您可以使用initWithCoder.
希望这可以帮助
山姆
归档时间: |
|
查看次数: |
2366 次 |
最近记录: |