baiju krishnan
baiju krishnan

Reputation: 241

how to add image in tableview from array

I want to add image on tableview. i create one UITableViewCell object. in .h file

@interface MainView1 : UITableViewCell{

IBOutlet UILabel *cellText;

IBOutlet UIImageView *productImg;

IBOutlet UILabel *cellText1;

}

- (void)LabelText:(NSString *)_text;

- (void)LabelText1:(NSString *)_text;

- (void)ProductImage:(NSString *)_text;

 @end

and .m file

- (void)LabelText:(NSString *)_text;{

  cellText.text = _text;
}

- (void)LabelText1:(NSString *)_text;{

     cellText1.text=_text;
 }


  - (void)ProductImage:(NSString *)_text;{

productImg.image = [UIImage imageNamed:_text];
  }

and in main file

//page contains table

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  MainView1 *cell = (MainView1 *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];
if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"MainView1" owner:self options:nil];
    cell = tableCell1;  
    [cell LabelText:[arryList objectAtIndex:indexPath.row]];
[cell ProductImage:[imgNameArray objectAtIndex:indexPath.row]];

    [cell LabelText1:[yesArray objectAtIndex:indexPath.row]];

}

image array is coming from database like this

 NSData   *imgdata=[[NSData alloc]initWithBytes:sqlite3_column_blob(cmp_sqlstmt,0)       length:sqlite3_column_bytes(cmp_sqlstmt,0)];

 [imgNameArray addObject:dataImage];

problem is only image is not showing error showing is unrecognized selector sent to instance 0x4e53940'

how to solve it Regards K L BAIJU

Upvotes: 0

Views: 494

Answers (1)

user1082071
user1082071

Reputation:

From the database, you get the image in form of NSData. You save it in your datasource array.

But then, you pass it as NSString

[cell ProductImage:[imgNameArray objectAtIndex:indexPath.row]];

where

- (void)ProductImage:(NSString *)_text;{

productImg.image = [UIImage imageNamed:_text];

}

You essentially need to change the the method parameter to NSData type

- (void)ProductImage:(NSData *) imageData{

    productImg.image = [UIImage imageNamed:_text];

}

But this still will not work. Because [UIImage imageNamed:] method tries to get the named image from the main bundle. But in your case you need an image from data you have. So your final method should look like this.

- (void)ProductImage:(NSData *) imageData{

        productImg.image = [UIImage imageWithData: imageData];

}

Also don't forget to change the method declaration in your header file to

- (void)ProductImage:(NSData *) imageData;

Upvotes: 5

Related Questions