2012-03-15 88 views
1

我有一个UITableViewCell,我添加了五个UIButtons。我以编程方式创建按钮,添加一个动作和一个图像。按钮被放置在正确的位置,动作起作用,标签被设置,但是它们很清晰。图像不显示。UIButton不显示图像

这是我用来创建一个按钮,并将其添加到细胞是什么,这个代码是在tableView:cellForRowAtIndexPath:

if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) { 
      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
      button.frame = CGRectMake(11, 6, 21, 21); 
      button.tag = 1; 
      [button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside]; 
      [button setImage:[UIImage imageNamed:@"Comp-Completed.png"] forState:UIControlStateNormal]; 
      [button setContentMode:UIViewContentModeScaleToFill]; 
      [cell.contentView addSubview:button]; 
     } 
     else { 
      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
      button.frame = CGRectMake(11, 6, 21, 21); 
      button.tag = 1; 
      [button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside]; 
      [button setImage:[UIImage imageNamed:@"Comp-Uncompleted.png"] forState:UIControlStateNormal]; 
      [button setContentMode:UIViewContentModeScaleToFill]; 
      [cell.contentView addSubview:button]; 
     } 

我试着添加:[button setEnabled:YES];[button setHidden:NO];[button setNeedsDislpay];到没有运气的过程。如何获取显示图像的按钮?

编辑: 我的形象存在,创造了UIImage,我可以拉UIImageUIButton的,并将其保存到一个文件中。我已经通过UIButton的每个属性来控制外观并更改它们。我也尝试了UIButtonTypeRoundedRectUIButtonTypeAddContact类型的空UIButton。我现在认为这与UITableViewCell不喜欢UIButtons有关,或者我如何将UIButton添加到UITableViewCell

+0

确定图像名称是否正确(包括大小写)并且图像文件包含在项目中? – jrturton 2012-03-15 17:36:48

+0

是的图像名称是正确的,我用不同的UITableView中的附件视图相同的图像 – 2012-03-15 17:44:34

+0

您确定该单元格显示按钮?尝试设置它的bacgroundColor以确保... – 2012-03-15 20:04:46

回答

0

它可以绝对找到图像吗?他们是否包含在项目中?

只是为了测试,尝试使用您已经在其他地方使用的图像,而不是Comp-Completed.png - 如果有效,您知道问题在于它无法找到该文件。

+0

我使用不同的图像,但它仍然无法正常工作。我也加倍检查了图像名称,并且它们是正确的。 – 2012-03-15 17:46:31

0

检查您的图像是否存在并导入。此外,它看起来除了图像名称代码是相同的。有点清洁剂为:

UIImage *buttonImage; 
if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) { 
    buttonImage = [UIImage imageNamed:@"Comp-Completed.png"]; 
} else { 
    buttonImage = [UIImage imageNamed:@"Comp-Uncompleted.png"]; 
} 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(11, 6, 21, 21); 
button.tag = 1; 
[button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside]; 
[button setImage:buttonImage] forState:UIControlStateNormal]; 
[button setContentMode:UIViewContentModeScaleToFill]; 
[cell.contentView addSubview:button]; 
+0

图像存在并被导入,我在不同的UITableView中使用相同的图像,并在那里工作。 (但我没有把它们放在UIButton中。) – 2012-03-15 17:48:59

0
UIImage * normalImage = [UIImage imageNamed:@"Comp-Uncompleted.png"]; 
UIImage * selectedImage = [UIImage imageNamed:@"Comp-Completed.png"]; 

UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 
button.frame = CGRectMake(11, 6, 21, 21); 
button.tag = 1; 
[button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside]; 
[button setImage:normalImage forState:UIControlStateNormal]; 
[button setImage:selectedImage forState:UIControlStateSelected]; 
[cell.contentView addSubview:button]; 

if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) { 
    button.selected = YES; 
} else { 
    button.selected = NO; 
} 
6

确保复制文件时,你已检查项目作为目标成员。它帮助我解决了这个问题。

0

我知道这是一个旧帖子,但我遇到了同样的问题,这是由于isHiddenisUserInteractionEnabled设置为隐藏按钮的值。

您必须小心将它们设置为YESNO

1

我的错误是将图像拖入作为pod的一部分的images.xcassets文件中,而不是主项目。它在故事板中显示得很好,但没有出现在应用程序中。