zzzzz
zzzzz

Reputation: 1239

Unable to add image in an nstableview cell?

I have tried this so far but on image is displayed in tableview.

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSLog(@"Entered to create a new cell for table");
    // Get a new ViewCell
    NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
    if ([tableColumn.identifier isEqualToString:@"country"]) 
    {
         cellView.textField.stringValue = [[[AppConstants sharedInstance].CountryList objectAtIndex:row]valueForKey:@"country"];
    }
    else
    {
          NSImage* img=[[NSImage alloc]init];
          img=[NSImage imageNamed:@"usa-flag.png"];
         [[cellView imageView] setImage:img];
   }
   return cellView;
}

How can I add images in a column.The images are small but there will only be an image in a column.Text will be in other columns.

EDIT: Do I have to make some modifications to the nib file ? I have only added a new tablcecolumn.My tableview is view based.

Upvotes: 1

Views: 2440

Answers (2)

zzzzz
zzzzz

Reputation: 1239

I was able to do this By removing the original cellview and adding Image and text table cell view.

Upvotes: 0

Hussain Shabbir
Hussain Shabbir

Reputation: 14995

Try like this just change your else block like this below-

  NSImage* img=[NSImage 
  imageNamed:@"usa-flag.png"];

//now set into imageview 
 imgView=[[NSImageView       
  alloc]init];
   [imgView setImage:img];

     cellView.imageView.image=imgView.image;

Upvotes: 1

Related Questions