maxname
maxname

Reputation: 5

How to add image on UITableView cells?

How can I add one image on several cells of UITableView?

I add an image to one top cell, like this:

screenshot

But after scroll to bottom and top I see this:

screenshot

Second and third cell has a z-index more that first cell.

How I can do this?

Thanks..

Upvotes: 0

Views: 319

Answers (9)

Ambroise Collon
Ambroise Collon

Reputation: 3869

Use addSubview on tableview directly.

let imageView = UIImageView(frame: CGRectMake(0, 0, width, height))
imageView.image = UIImage(named: "My Image")
self.tableView.addSubview(imageView)

Upvotes: 0

Cocoanetics
Cocoanetics

Reputation: 8255

You wouldn't add it to a single cell, but to the tableview itself. Then - in a tableview subclass - you would make sure that in the layoutSubviews the image view stays on top and the position gets adjusted to stay in a fixed place. Or next to a specific cell if you want that.

Upvotes: 0

maxname
maxname

Reputation: 5

I did it.

  1. Add UIImageView (let's say img1) object to UITableView (table1)
  2. Set img1.Layer.ZPosition to 1 that the image was above cells in the table1
  3. For Headers in sections of table set img1.Layer.ZPosition to 2 that a header was above an image

Thanks all for help!

Upvotes: 0

Vinodh
Vinodh

Reputation: 5268

You can add image as subview to table

UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imgView.image=[UIImage imageNamed:@"img.png"];
[UITableview addSubview:imv];

or

you can increase the height of cell

Upvotes: 0

CNU
CNU

Reputation: 134

if you want to add the image on the tableView, get the scroll view instance from the tableview using tableviewobj.scrollview and then add the image as subview on the scrollview and make the image come front using bringsubviewtofront method.

Upvotes: 0

Krishna Kumar
Krishna Kumar

Reputation: 1652

If you want to add image on cell then I would recommend you to make custom cell with imageView .

Upvotes: 0

EnriMR
EnriMR

Reputation: 3970

Are you adding the image to the CellView or to the TableView?

You can try the following function in which you have to specify the filename, position of the picture, size and view to which you want to add it.

- (void)createImage:(NSString *)name
                   inX:(NSInteger)x
                   inY:(NSInteger)y
                 width:(NSInteger)w
                height:(NSInteger)h
                  view:(UIView *)v{

    UIImage *background = [UIImage imageNamed:name];
    CGRect rect = CGRectMake(x, y, w, h);
    UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:rect];
    yourImageView.image = background;
    [v addSubview:yourImageView];
}

Upvotes: 1

Maulik Kundaliya
Maulik Kundaliya

Reputation: 462

Adjust your cell height as a image size.

Upvotes: 0

Jitendra
Jitendra

Reputation: 5081

UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imgView.image=[UIImage imageNamed:@"img.png"];
[cell addSubview:imv];

neither you can directly add image to cell

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

   UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil)
      cell = [[[UITableViewCell alloc] UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

   cell.textLabel.text = @"I'm a UITableViewCell!";
   cell.imageView.image = [UIImage imageNamed:@"image.png"];

   return cell;
}

try this code....

and adjust the image size....

Upvotes: 1

Related Questions