2013-03-20 124 views
0

嗨我有一个向按钮添加图像的问题。图像存储在一个NSMutableArray属性。问题是添加并显示图像到按钮。我已验证图像已添加到阵列中。我从照相机的图像后,然后我选择要使用它应用将用下面的错误崩溃:不能将图像添加到按钮

*终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:“ - [UIImage的长度]:无法识别的选择发送到实例0x2090b5a

代码:

displayArray=[[NSMutableArray alloc]initWithObjects:@"MedsTime_MedPic.png",@"",@"",@"", nil]; 

-(void)addImage1:(NSNotification*)notify 
{ 
    butImage=xapp.medicineImage; 
    [img setImage:xapp.medicineImage forState:UIControlStateNormal]; 
    [displayArray replaceObjectAtIndex:0 withObject:xapp.medicineImage ]; 
    [table reloadData]; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 

    } 
    else 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 
    } 

    if (indexPath.section==0) 
    { 
     img=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     img.frame=CGRectMake(25, 15, 75, 75); 
     img.layer.cornerRadius=6.0; 
     img.clipsToBounds=YES; 
     img.layer.borderColor=[[UIColor grayColor]CGColor]; 
     [cell.contentView addSubview:img]; 
     if (xapp.isEdited==YES) 
     { 

     } 
     else if (xapp.isEdited==NO) 
     { 
      [img setImage:[UIImage imageNamed:[displayArray objectAtIndex:0]] forState:UIControlStateNormal]; 

     } 

     [cell.contentView addSubview:img]; 

     img.titleLabel.textColor=[UIColor blackColor]; 
     text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)]; 
     text.delegate=self; 
     [email protected]"Enter Medicine First"; 

     if (xapp.isEdited==YES) 
     { 
      text.text=[[editArray objectAtIndex:0]valueForKey:@"medicineName"]; 
     } 
     else{ 

      text.text=[displayArray objectAtIndex:1]; 
     } 


     [text setBorderStyle:UITextBorderStyleRoundedRect]; 
     [cell addSubview:text]; 
     [cell addSubview:img]; 


    } 
} 
+0

为什么要向单元格和contentView添加'img'对象?另外,你为什么要将img添加到contentView两次? – Petar 2013-03-20 12:50:48

+1

添加什么它完全崩溃? – Pfitz 2013-03-20 12:55:49

+1

你也可以在didSelectRowForIndexPath中发布代码吗? (我想这是发生崩溃的地方) – Petar 2013-03-20 12:57:18

回答

0

替换你的code.Add一个新的数组,然后check.Hope它对你有用。

if (xapp.isEdited==YES) 
     { 
      CamImage*eventImage = (CamImage*)[editArray objectAtIndex:0]; 
      [img setImage:[eventImage valueForKey:@"medicineImage"] forState:UIControlStateNormal]; 
      } 

     else if (xapp.isEdited==NO) 
      NSLog(@"editedmo"); 
     { 
      if(Medimage==NO){ 
       UIImage *i=(UIImage*)[displayArray objectAtIndex:0]; 
       [img setImage:i forState:UIControlStateNormal]; 
      [img setImage:[UIImage imageNamed:[displayArray objectAtIndex:0]] forState:UIControlStateNormal]; 
       } 
0

你应该知道你不能添加图片到UIButtonTypeRoundedRect,你应该将其更改为自定义按钮。

而在此功能,您用图像替换显示阵列的第一个项目,而在显示阵列其他项目NSString的这似乎是问题的根源:

-(void)addImage1:(NSNotification*)notify 
{ 
    butImage=xapp.medicineImage; 
    [img setImage:xapp.medicineImage forState:UIControlStateNormal]; 
    [displayArray replaceObjectAtIndex:0 withObject:xapp.medicineImage ]; 
    [table reloadData]; 
} 
从一些地方

除了我不明白你的代码。有一些明显的错误:

这部分与uitableview惯例冲突,你可以删除它没有得到错误。

else 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 
} 

您将'img'添加到contentView两次,一次就够了。

0

您(或在SDK一些方法)所要求的图像执行该方法length

reason: '-[UIImage length]: unrecognized selector sent to instance 0x2090b5a

UIImage不具有方法的长度。根据文件,这是一种NSString和/或UIAElementArray的方法。

我的猜测是你正在使用的图像,其中NSStringUIAElementArray预计。