0

I want to place my image in location (10,10,60,60). I write the code but image display from corner. The code [cell.imageView setFrame:CGRectMake(10,10,60,60)]; has no effect.

if(indexPath.row==0)
{
    NSString *filepath=[[NSBundle mainBundle]pathForResource:pro.s_image ofType:@"png"];
    UIImage *image=[UIImage imageWithContentsOfFile:filepath];

    //cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

    //[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];

    //[cell.imageView setFrame:CGRectMake(50,50,100,100)];

    [cell.imageView setFrame:CGRectMake(10,10,60,60)];

    //  CGRect iframe = cell.imageView.frame;
    //  iframe.size.height = 100.0;
    //  iframe.size.width=100.0;
    //  cell.imageView.frame = iframe;

    cell.imageView.image=image;
}
GauravBoss
  • 140
  • 2
  • 4
  • 13

3 Answers3

1

One option is to subclass UITableViewCell and setting the location of the image in the subclass.

Here's a similar post except they're trying to customize the location of the text label:

iPhone UITableViewCell: repositioning the textLabel

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • @thanks bryanmac..... i am trying to do it.... please give me little explanation for -(void) layoutSubviews... – GauravBoss Nov 07 '11 at 09:29
0

The lazy (bad practive) solution instead of subclassing is to a add a UIImageView to the cell's contentView (and ignore cell.imageView):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ....
    UIImageView * myCreatedImageView=(UIImageView *)[cell.contentView viewWithTag:12345];
    if (myCreatedImageView==nil) {
      myCreatedImageView=[[UIIimageView alloc]...]
      myCreatedImageView.tag=12345;//identifying tag number
      [self.contentView addSubview:myCreatedImageView];
    }

    //code to manage the image view:
    myCreatedImageView.frame=...
    myCreatedImageView.image=...
}
RabinDev
  • 658
  • 3
  • 13
0

You may want to try to switch the last two lines. Like this:

cell.imageView.image=image;
[cell.imageView setFrame:CGRectMake(10,10,60,60)];

The cell.imageView may be nil before you provide the image.

mr.pppoe
  • 3,945
  • 1
  • 19
  • 23