Reputation: 3071
I have added a uibutton in my uitableviewcell and trying to change its background image when it is taped here is the code
-(void)downloadImage:(UIButton *)link
{
UITableViewCell *cell = (UITableViewCell*)[link superview];
UIButton *view = [[UIButton alloc]init];
NSArray *subviews = [cell subviews];
for (view in subviews)
{
if([view isKindOfClass:[UIButton class]])
{
view = (UIButton*)subviews;
[view setBackgroundImage:[UIImage imageNamed:@"yellow"] forState:UIControlStateNormal];
}
}
...
but its not working
if i add this line
view = (UIButton*)subviews;
i am getting this error
Thread 1:signal SIGTRAP
and without this line nothing is happening, any idea what is going wrong?
Upvotes: 0
Views: 147
Reputation: 11607
It depends on how you're adding your subview to your UITableViewCell in your:
-(UITableViewCell)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeReusableCellForID:cellID];
if(cell == nil)
{
...
myButton = [[UIButton alloc] initWithFrame....];
[myButton addTarget:self selector:@selector(downloadImage:) forControlEvent:UIControlEventTouchUpInside];
// -------------------------------------------------------
// This is the important part here.
// Usually, we add "myButton" to the cell's contentView
// You will need to match this subview hierarchy
// in your "downloadImage:" method later
// -------------------------------------------------------
[cell.contentView addSubview:myButton];
}
return cell;
}
-(void)downloadImage:(id)sender
{
// -------------------------------------------------------
// Here, "sender" is your original "myButton" being tapped
//
// Then "[sender superview]" would be the parent view of your
// "myButton" subview, in this case the "contentView" of
// your UITableViewCell above.
//
// Finally, "[[sender superview] superview]" would be the
// parent view of the "contentView", i.e. your "cell"
// -------------------------------------------------------
UIButton *button = (UIButton *)sender;
// button now references your "myButton" instance variable in the .h file
[button setBackgroundImage:[UIImage imagenamed:@"filename.png"] forControlState:UIControlStateNormal];
}
Hope that helps.
As for why your app crashes when you do:
view = (UIButton*)subviews;
That is because "subviews" is an NSArray of all the subviews. You're telling iOS to typecast an NSArray * to a UIButton *, which it doesn't know how to do. So you end up causing the app to crash.
In your for loop, you probably want to do something like this instead (although you shouldn't need to if you use my above "downloadImage" method):
for (view in subviews)
{
if([view isKindOfClass:[UIButton class]])
{
// ------------------------------------------------
// When you go for(view in subviews), you're saying
// view = [subviews objectAtIndex:i]
//
// Hence view = (UIButton *)subviews become
// redundant, and not appropriate.
// ------------------------------------------------
//view = (UIButton*)subviews; // <--- comment out/delete this line here
[view setBackgroundImage:[UIImage imageNamed:@"yellow"] forState:UIControlStateNormal];
}
}
Upvotes: 2