2012-09-05 43 views
1

我无法添加按钮,我UITableViewCell,该单元有两个UILabel秒和2个UIImageView S,有时UIImageView将包含一个图像,有时一个按钮:我的UITableViewCell设置按钮

enter image description here

在我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; 
} 

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]; 

添加一个按钮,摄像画面死机的子视图:

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: 

我缺少什么?

谢谢!

只需添加!其重要的这一行

[firstLockImage setUserInteractionEnabled:YES]; 
    [secondLockImage setUserInteractionEnabled:YES]; 

因为UIImageView有没有作为默认和按钮不工作没有它!

+0

上午我错过了什么?我看不到你在代码中创建(重用)单元本身的位置。这个动作通常在tableView中完成:cellForRow: – Leonardo

+0

我只复制相关代码 – user1256477

回答

5

如果您阅读崩溃错误,可以很容易地看到问题出在哪里。你还没有初始化你的按钮。

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,0,0)]; 

或者你可以使用initWithCoder。

希望这有助于

山姆

1

阅读异常文本 - 它说:

这种观点可能没有收到initWithFrame:方法或利用initWithCoder:在你的问题

短短的几行,你要发送消息一个UIButton实例,您只有alloc'd并且未发送任何init...消息。那是你的错误。

此外,你不应该直接调用 UIButtonalloc/ init对,因为它是一类集群,你通常应该使用 +[UIButton buttonWithType:]得到一个按钮实例。

编辑实际上,我不是100%确定的。但是,如果你做的是initWithFrame:,你并不确切知道你会得到什么,所以我仍然用buttonWithType:去得到一个自定义按钮,这是你想要的。 编辑完

因此,该行更改为:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

希望这有助于!

+0

非常好的解释!我试过了,如果我按照你所说的那样初始化按钮,我必须添加button.frame来设置框架。 – user1256477

+1

谢谢!是的,你必须设置框架。你也可以在设置图像后执行'sizeToFit',这可能会给你一个合理的默认开始。 –

1

变化UIButton *button =[UIButton alloc];ALLOC W/O初始化?)到

UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom]; 
//set frame 
[button setFrame:(CGRect){x,y,w,h}]; 

+buttonWithType处理分配/初始化您的UIButton对象