Reputation: 5970
I have an app where there is a UITableView
populated UIButtons that are created programatically. The code below shows how one UIButton
is created.
It works perfectly on the simulator but not on the device. On the simulator the buttons are displayed with the buttonImage.png shown but on the device the image is not there. All other aspects of the buttons are correct.
The image has been added to the resources correctly as far as I can tell. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
if (cell ==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0,0,160, 160)];
[button setBackgroundImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:19/255.0f green:73/255.0f blue:17/255.0f alpha:1] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"arial" size:20];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"button" forState:UIControlStateNormal];
//more buttons created as needed
NSMutableArray *buttonArray = [[NSMutableArray alloc] init];
[array addObject:button];
//more buttons added to array as needed
[cell addSubview:[buttonArray objectAtIndex:indexPath.row]];
return cell;
}
Upvotes: 2
Views: 2417
Reputation: 854
Check spelling and also allow user interaction by button.allowUserInteraction = true
Upvotes: 0
Reputation: 21
I ran into a similar problem when I renamed an icon in my application, using the XCode navigator window. All was fine in the simulator, but when I (eventually) tested on my device the icon would not show up. I verified the spelling, and case, numerous times (after reading the above accepted answer), and I even copy/pasted the icon name over... no dice.
I tried cleaning the app and rebuild... no dice.
Finally I closed XCode completely and relaunched from 'Launchpad',... and it worked.
It was a frustrating hour. Feels, to me, like there's a bug in there somewhere.
Upvotes: 2
Reputation: 5667
Your code is not at fault. I am sure you have tested your app on non retina simulator & it is showing button.
So now when you test it on device, I am sure you are testing on a retina device & you have not provided the 2x version of your button image.
Solution:: add a 2x version of image to your resource.
Example:: yourImage.png (32x32), then add another image to resource with name [email protected] (64x64)
That solves your problem.
Hope that helps.
Upvotes: 1
Reputation: 2139
Try to make these changes in your code:
Change control event
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchInside];
Add Button in content view of cell
[cell.contentview addSubview:[buttonArray objectAtIndex:indexPath.row]];
Upvotes: 0
Reputation: 567
Check your image name, because simulator is not case sensitive but the device is case sensitive.
Upvotes: 10