Tirthendu
Tirthendu

Reputation: 65

How to populate a UITableViewCell with more than one item?

I am totally new in iOS developing. I have a custom cell in my tableview. I have 32 images stored in a group named Images. Now I want to populate my tableview in such a way that one row of my cell contains 4 images. I am just a bit confused how to design it. Please help me.

This is my code..

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

 self.patternsArray = @[@"1.jpg",@"3.jpg",@"2.jpg",@"4.jpg",@"6.jpg",@"5.jpg",@"7.jpg",@"9.jpg",@"8.jpg",@"10.jpg",@"12.jpg",@"11.jpg",@"13.jpg",@"15.jpg",@"14.jpg",@"16.jpg", @"18.jpg",@"17.jpg",@"19.jpg",@"21.jpg",@"20.jpg",@"22.jpg",@"24.jpg",@"23.jpg", @"25.jpg",@"27.jpg",@"26.jpg",@"28.jpg",@"30.jpg",@"29.jpg",@"32.jpg",@"31.jpg"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];

_patternsString = [self.patternsArray objectAtIndex:indexPath.row];



UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
imageView1.image = [UIImage imageNamed:_patternsString];


return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [self.patternsArray count];

}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Upvotes: 0

Views: 59

Answers (3)

AHSAN NAZIR RAJA
AHSAN NAZIR RAJA

Reputation: 166

This is how i have populated 2 images in one row you can follow these line to populate 4 image in one row , like take remainder of array count with 4 and if remainder is 0 then total number of rows will be array.count/4 .   


  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {

        if ([[Globals sharedInstance].couponsArray count]%2==0)
        {
            return [[Globals sharedInstance].couponsArray count]/2;
        }
        else
        {
            return [[Globals sharedInstance].couponsArray count]/2+1;
        }



    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *CellIdentifier = @"CustomCell1";
        //    int currentRow = [indexPath row];


        CustomCell1 *cell = (CustomCell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
        if (cell == nil){
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomCell1" owner:self options:nil];
            for (id object in nib) {
                if([object isKindOfClass:[CustomCell1 class]])
                    cell = (CustomCell1*)object;
            }

        }

        if (cell == nil)
        {
            cell = [[CustomCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSLog(@"here is indexPath %d",indexPath.row);

        int index= indexPath.row*1+indexPath.row;
        int index1= index+1;


        if ([Globals sharedInstance].couponsArray.count/2>=indexPath.row)
        {
        [cell.indicater1 stopAnimating];
        [cell.indicater2 stopAnimating];
        }

        NSString* imageURLString = [[[Globals sharedInstance].couponsArray objectAtIndex:index] objectForKey:@"coupon_image"];
        NSURLRequest* imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString]
                                                      cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                  timeoutInterval:60];

         [cell.indicater1 setCenter:cell.carImage.center];
         [cell.indicater1 startAnimating];

        [cell.carImage setImageWithURLRequest:imageRequest
                             placeholderImage:nil
                                      success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
        {
                                          [cell.indicater1 stopAnimating];
                                          [cell.carImage setImage:image];

                                          NSLog(@"Image must be Loaded here");
                                      }
                                      failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                          [cell.indicater1 stopAnimating];
                                      }];





        [cell.btn1 setTag:index];
        [cell.btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];





        if (index1>[Globals sharedInstance].couponsArray.count-1)
        {


            cell.carImage1.hidden=YES;
            cell.lblModel_car1.hidden=YES;
            cell.secondndImage.hidden=YES;
            cell.indicater2.hidden=YES;
            cell.indicater1.hidden=YES;
            cell.triangleImage.hidden=YES;


        }

        else
        {

            NSString* imageURLString1 = [[[Globals sharedInstance].couponsArray objectAtIndex:index1] objectForKey:@"coupon_image"];
            NSURLRequest* imageRequest1 = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString1]
                                                           cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                       timeoutInterval:600];

             [cell.indicater2 setCenter:cell.carImage1.center];
             [cell.indicater2 startAnimating];

            [cell.carImage1 setImageWithURLRequest:imageRequest1
                                  placeholderImage:nil
                                           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                               [cell.indicater2 stopAnimating];
                                               [cell.carImage1 setImage:image];

                                               NSLog(@"Image must be Loaded here");
                                           }
                                           failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                               [cell.indicater2 stopAnimating];
                                           }];



            [cell.btn2 setTag:index1];
            [cell.btn2 addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];

        }


        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;


    }

Upvotes: 1

Robin
Robin

Reputation: 87

    you have to add 4 imageview into tableviewcell

for this you need a customtableviewcell then add indexPath.row=4*indexPath.row; imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]; imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]; imageView3.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]+2; imageView4.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+3];

Upvotes: 0

robygam
robygam

Reputation: 51

Why don't you set image view of uitableViewCell? Try this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];

_patternsString = [self.patternsArray objectAtIndex:indexPath.row];

cell.imageView.image = [UIImage imageNamed:_patternsString];

return cell;
}

Hope it helps you. If you are designing a custom cell (not using system imageView of tableViewCells), probably you need create a custom TableViewCell.

Upvotes: 0

Related Questions