使用UIImageView的自定义UIButton在设备上无法正常工作

时间:2010-01-25 08:31:54

标签: iphone objective-c uitableview

我的问题是我有一个自定义按钮和ImageView,我添加了Image.I将此imageView作为子视图的自定义按钮。当我在模拟器中运行它工作正常,但当我在设备中运行它没有在按钮中显示图像。

我把代码编写为:

在cellForRowtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MasterViewIdentifier"];
 if (cell == nil) 
 {

  cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MasterViewIdentifier"] autorelease];
  cell.selectionStyle = UITableViewCellSelectionStyleNone;

  UIView* elementView =  [[UIView alloc] initWithFrame:CGRectMake(20,170,320,280)];
  elementView.tag = 0;
  elementView.backgroundColor=[UIColor clearColor];
  [cell.contentView addSubview:elementView];
  [elementView release];

 }
 UIView* elementView  = [cell.contentView viewWithTag:0];
 elementView.backgroundColor=[UIColor clearColor];
 for(UIView* subView in elementView.subviews)
 {
  [subView removeFromSuperview];
 }
         if(indexPath.section == 8)
   {
            imageView.image = [UIImage imageNamed:@"yellow_Star.png"];
            MyCustomButton *button1 = [[MyCustomButton alloc]initWithRating:1];  
  [button1 setFrame:CGRectMake(159, 15, 25, 20)];
  [button1 setShowsTouchWhenHighlighted:YES];
  [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  [button1 addSubview:imageView];
  [elementView addSubview:button1];
  [button1 release];
  [imageView release];
         }
return cell;
}

并在我的buttonAction中:

-(void)buttonAction:(MyCustomButton*)sender
{
 rating = [sender ratingValue];
 event.eventRatings = rating;
 [tableView reloadData];

}

和我MyCustomButton类: 我有一个方法

-(MyCustomButton*) initWithRating:(NSInteger)aRatingValue
{
 if (self = [super init])
 {
  self.ratingValue = aRatingValue;
 }
 return self;
}

请帮助解决问题的人。

谢谢你, Monish。

1 个答案:

答案 0 :(得分:0)

您正在尝试加载名为“yellow_Star.png”的图像 - 检查它是否实际命名为(具有相同的字符大小写)。 iPhone上的文件系统区分大小写,而Mac OS则不然 - 因此通常会导致此类问题,并且首先要查看文件名。

编辑:您还可以尝试检查应用程序包中是否存在该图像 - 可能是它是从复制资源构建中逐步删除的,也就是说您的图像必须位于“复制捆绑资源“构建目标的构建步骤。如果没有,您可以将图像拖放到那里。