Vipin
Vipin

Reputation: 4728

Adding images to a cell in table view

I am creating an app in which i want to display a table view and each cell of table view will be displaying 5 different images.Let me say in detail.I have 10 images and i want to show them in a table view .say 4 images in first row of table view then next 4 in next row ,then the remaining two in next row.Being a newbie i am unable to get a startup.Please help.Any help will be appreciated.

Thanks, Christy

Upvotes: 1

Views: 3023

Answers (2)

Robin
Robin

Reputation: 10011

I think for startup you can go over here

How to display the UITableView programmatically?

Ok if you know how to load a uitableview than I think you can get started with complex part.

You need to add this line of code in your cellForRow method

You need to set the frame size and image name according to your requirement

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier];
    }
    if(indexPath.row == 0)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image1.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image2.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image3.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image4.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];
    }
    else if(indexPath.row == 1)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image5.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image6.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image7.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image8.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];
    }
    else if(indexPath.row == 2)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image9.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image10.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];        
    }
    else
    {
        // Do something here
    }
    return cell;
}

Edit:

@Christy you can have a button that can make a table view scroll. But iphone users are used to see the scroll bar on the right of the table to see if the table has any more data or not.

But if you want to have a Button that scrolls the table than here is the code

In your cellForRow method of tableView in this block else if(indexPath.row == 2){} add a UIButton with a down arrow like this

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220.0f, 5.0f, 150.0f, 38.0f)];
[button setImage:[UIImage imageNamed:@"Arrow"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
[button release];

and in the selector method

- (void)doSomething
{
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]
}

Upvotes: 1

Aravindhan
Aravindhan

Reputation: 15628

    IBOutlet UIImageView *image,*image12;

    In .m file
cellforrowAtIndexPath
{  
  UIImage *image1=[UIImage imageNamed:@"name.jpg"];
    image=[[UIImageview alloc]initWithImage:image1];
    image.frame=CGRectMake(0,0,50,50);

    [cell addSubview:image];

UIImage *image13=[UIImage imageNamed:@"name.jpg"];
    image12=[[UIImageview alloc]initWithImage:image13];
    image12.frame=CGRectMake(50,0,50,50);

    [cell addSubview:image12];

}

Upvotes: 1

Related Questions