【发布时间】:2014-02-12 11:34:01
【问题描述】:
我有一个应用程序,其中有一个以编程方式创建的 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;
}
【问题讨论】:
-
试试
[cell.contentview addSubview:[buttonArray objectAtIndex:indexPath.row]]; -
嘿!您必须检查一件愚蠢的事情,这可能是您的问题。检查名称的大小写应该相同。例如:- buttonImage 或 buttonimage 它应该是相同的名称,相同的大小写。否则它将显示在模拟器而不是设备上。
-
这可能是您的代码中的问题:stackoverflow.com/a/6773296/817365
标签: ios objective-c uibutton