【问题标题】:Issue in adding image to the UIButton.将图像添加到 UIButton 时出现问题。
【发布时间】:2010-02-01 08:40:08
【问题描述】:

我在申请中遇到了另一个问题,我为此浪费了很多时间。 有没有人可以帮忙解决这个问题。

实际上我有一个事件,我应该为那个事件打分,因为我写的代码是:

在 CellForRowAtIndexPath ......我的代码为:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MasterViewIdentifier"];
    //UITableViewCell *cell = nil;
    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)
     {
      UIImage *whiteImg = [UIImage imageNamed:@"white_star.png"] ;
      UIImage *yellowImg = [UIImage imageNamed:@"yellow_Star.png"] ;
      UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(159, 15, 25, 20)];
      [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
      button1.tag = 1;
      UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(185, 15, 25, 20)];
      [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
      button2.tag = 2;
      UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(211, 15, 25, 20)];
      [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
      button3.tag = 3;
      UIButton *button4 = [[UIButton alloc]initWithFrame:CGRectMake(237, 15, 25, 20)];
      [button4 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
      button4.tag = 4;
      UIButton *button5 = [[UIButton alloc]initWithFrame:CGRectMake(263, 15, 25, 20)];
      [button5 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
      button5.tag = 5;
      if(event.eventRatings == 1)
      {

       [button1 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button2 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button3 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button4 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button5 setBackgroundImage:whiteImg forState:UIControlStateNormal];

      }
      else if(event.eventRatings == 2)
      {
       [button1 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button2 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button3 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button4 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button5 setBackgroundImage:whiteImg forState:UIControlStateNormal];       
      }
      else if(event.eventRatings == 3)
      {
       [button1 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button2 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button3 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button4 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button5 setBackgroundImage:whiteImg forState:UIControlStateNormal];        
      }
      else if(event.eventRatings == 4)
      {

       [button1 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button2 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button3 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button4 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button5 setBackgroundImage:whiteImg forState:UIControlStateNormal];

      }
      else if(event.eventRatings == 5)
      {
       [button1 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button2 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button3 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button4 setBackgroundImage:yellowImg forState:UIControlStateNormal];
       [button5 setBackgroundImage:yellowImg forState:UIControlStateNormal];
      }
      else
      {
       [button1 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button2 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button3 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button4 setBackgroundImage:whiteImg forState:UIControlStateNormal];
       [button5 setBackgroundImage:whiteImg forState:UIControlStateNormal];
      }

      [elementView addSubview:button1];
      [button1 release];
      [elementView addSubview:button2];
      [button2 release];
      [elementView addSubview:button3];
      [button3 release];
      [elementView addSubview:button4];
      [button4 release];
      [elementView addSubview:button5];
      [button5 release];

      if(isRightButton == YES)
      {
       button1.enabled = NO;
       button2.enabled = NO;
       button3.enabled = NO;
       button4.enabled = NO;
       button5.enabled = NO;       
      }
      else if(isRightButton == NO)
      {
       button1.enabled = YES;
       button2.enabled = YES;
       button3.enabled = YES;
       button4.enabled = YES;
       button5.enabled = YES;       
      }


      [elementView addSubview:ratingsTitleLabel];
      cell.accessoryType = UITableViewCellAccessoryNone;
     }
return cell;
}

And the action of the button is written as:

    -(void)buttonAction:(id)sender
    {
     rating = [sender tag];
     printf("\n Ratig Value inside Button Action~~~~~~~~~~~~~~~~%d",rating);
     event.eventRatings = rating;
     [tableView reloadData];    
    }

当我在 3.1.2 OS 的模拟器中构建应用程序时,它通过显示星形图像可以正常工作。 我的问题是当我在 3.1.2 OS 设备中构建它时,图像未显示。我检查了文件名及其 gud 中是否区分大小写的代码,但我不明白要显示的图像。

大家帮我解决这个问题。

谢谢, 莫尼什库马尔。

【问题讨论】:

  • 大家好......帮我解决这个问题。
  • 图片加载正确吗?如果您的 UIImage 实例不为零,请检查调试。尝试检查应用程序包中是否缺少图像。
  • 是的,图像正在加载,并且 UIImage 实例不是 nil。图像也存在于应用程序包中。
  • 设备中仍然没有显示图像。

标签: iphone objective-c uiimage


【解决方案1】:

试试这个: - 从 iPhone 中删除您的应用 - 从构建菜单中清除所有标签 - 将您的图像复制到另一个目录 - 从项目中删除图像(删除时单击“也移至垃圾箱”) - 再次添加您的图像 - 构建并运行

希望它有效。

【讨论】:

  • 嗨,Dreiu,我按照你的建议尝试了所有的事情......仍然没有到来。我不知道它正在发生这样的事情。你能建议我任何其他想法吗?
  • 你试过用其他图片吗?
  • 是的,我尝试使用其他图像,它在设备中工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
相关资源
最近更新 更多