My UITableViewCell customization button
I am having trouble adding a button to mine UITableViewCell
, a cell has two UILabel
and two UIImageView
s, UIImageView
will sometimes contain an image and sometimes a button:
In my subclass UITableViewCell
, I have:
- (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;
}
When one UIImageView
is just an image no problem but it crashes when I add UIButton
(imaged) as a subview.
In implementation 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];
Adding a button as a subtask for viewing images:
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:
What am I missing?
Thank!
Just add! his important line
[firstLockImage setUserInteractionEnabled:YES];
[secondLockImage setUserInteractionEnabled:YES];
As UIImageView is NO by default and the button doesn't work without it!
If you read the error on crash, it's pretty easy to see where your problem is. You haven't initialized your button.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,0,0)];
or you can use initWithCoder.
Hope it helps
Sam
Read the text of the exception - it says:
This view probably didn't get initWithFrame: or initWithCoder:
A couple of questions in your question, you are posting to an instance UIButton
that you only have alloc
'd and not sent any messages init...
. This is your mistake.
Also, you shouldn't directly call the alloc
/ init
on pair UIButton
as a class cluster, and you should usually use +[UIButton buttonWithType:]
to get the button instance.
EDIT I'm not really 100% sure. But you don't know exactly what you get if you do initWithFrame:
, so I'll go with help anyway buttonWithType:
to get the custom button you need. END EDIT
So, change this line to:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
Hope this helps!
change UIButton *button =[UIButton alloc];
( alloc w / o init ?) to
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
//set frame
[button setFrame:(CGRect){x,y,w,h}];
+buttonWithType
handles alloc / init for your UIButton object