我有一个应用程序,其中有一个UITableView
填充的UIButtons以编程方式创建。下面的代码显示了如何创建UIButton
。
它在模拟器上完美运行,但在设备上无效。在模拟器上显示的按钮显示 buttonImage.png ,但在设备上图像不存在。按钮的所有其他方面都是正确的。
据我所知,图像已正确添加到资源中。有什么想法吗?
- (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;
}
答案 0 :(得分:10)
检查您的图像名称,因为模拟器不区分大小写,但设备区分大小写。
答案 1 :(得分:2)
当我在应用程序中使用XCode导航器窗口重命名图标时遇到了类似的问题。在模拟器中一切都很好,但是当我(最终)在我的设备上测试时,图标不会显示出来。我已经多次验证了拼写和案例(在阅读了上面接受的答案之后),我甚至将图标名称复制/粘贴到了......没有骰子。
我尝试清理应用并重建......没有骰子。
最后我完全关闭了XCode并从'Launchpad'重新启动......,它确实有效。
这是一个令人沮丧的时刻。感觉,对我来说,就像那里有一个小虫。
答案 2 :(得分:1)
您的代码没有错。我相信你已经在非视网膜模拟器上测试了你的应用程序。它正在显示按钮。
所以现在当你在设备上进行测试时,我相信你正在测试视网膜设备和放大器。您还没有提供按钮图像的2x版本。
解决方案::将2x版本的图像添加到您的资源中。
示例:: yourImage.png(32x32),然后将另一个图像添加到具有名称的资源 yourImage@2x.png(64x64)
这解决了你的问题。
希望有所帮助。
答案 3 :(得分:0)
尝试在代码中进行这些更改:
更改control event
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchInside];
在单元格的内容视图中添加按钮
[cell.contentview addSubview:[buttonArray objectAtIndex:indexPath.row]];
答案 4 :(得分:0)
尝试从设备中删除已安装的应用,然后重新运行
答案 5 :(得分:0)
检查拼写并允许button.allowUserInteraction = true