0

I am using UIGridView as 2 Column is there and there are list of array in which images kept. Now I want to compare two car so that i have to mark any two images in UIGridView. So that I can compare any two cars. i am not fully aware of GridView.

Compare should be happened when red navigation button clicked.

My Code is Given Below:

   CarArray = [[NSMutableArray alloc]init];
   [CarArray addObject:@"image.png"];
   [CarArray addObject:@"car-image.png"];
   [CarArray addObject:@"mercy.png"];
   [CarArray addObject:@"lanbo.png"];

}

- (CGFloat) gridView:(UIGridView *)grid widthForColumnAt:(int)columnIndex
{
    return 160;
}

- (CGFloat) gridView:(UIGridView *)grid heightForRowAt:(int)rowIndex
{
    return 200;
}

- (NSInteger) numberOfColumnsOfGridView:(UIGridView *) grid
{
    return 2;
}

- (NSInteger) numberOfCellsOfGridView:(UIGridView *) grid
{
    return [CarArray count];
}

- (UIGridViewCell *) gridView:(UIGridView *)grid cellForRowAt:(int)rowIndex AndColumnAt:(int)columnIndex
{
    Cell *cell = (Cell *)[grid dequeueReusableCell];

    if (cell == nil) {
        cell = [[Cell alloc] init];
    }

    cell.thumbnail.image=[UIImage imageNamed:[CarArray objectAtIndex:(rowIndex*2+columnIndex)]];

    return cell;
}

- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex
{
    NSLog(@"%d, %d clicked", rowIndex, colIndex);
    CarDetailViewController *servicesViewController_obj=[[CarDetailViewController alloc]initWithNibName:@"CarDetailViewController" bundle:nil];
    [self.navigationController pushViewController:servicesViewController_obj animated:YES];

}

i also want to implement mark and unmark so i can move further.i have to show This mark sign (right symbol in image below)

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
JohnWick
  • 241
  • 2
  • 13

3 Answers3

0

I am assuming that you are using this UIGridView sample code.

I have done some changes in sample code for your requirement. Please follow steps to change images in grid view:-

1. In UIGridViewDelegate.h:-
Change the optional method
- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)columnIndex;
to
- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)columnIndex gridViewCell:(UIGridViewCell *)gridViewCell;

2. In UIGridView.m:-
Change this code

- (IBAction) cellPressed:(id) sender
{
    UIGridViewCell *cell = (UIGridViewCell *) sender;
    [uiGridViewDelegate gridView:self didSelectRowAt:cell.rowIndex AndColumnAt:cell.colIndex];
}

with

- (IBAction) cellPressed:(id) sender
{
    UIGridViewCell *cell = (UIGridViewCell *) sender;
    [uiGridViewDelegate gridView:self didSelectRowAt:cell.rowIndex AndColumnAt:cell.colIndex gridViewCell:cell];
}


3. In RootViewController.m:-
Add this code

- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex gridViewCell:(UIGridViewCell *)gridViewCell
{
    NSLog(@"%d, %d clicked", rowIndex, colIndex);

    Cell *cellTemp = (Cell *)gridViewCell;
    UIImage *imgT = cellTemp.thumbnail.image;


    // Change the image names according to your images
    if (imgT == [UIImage imageNamed:@"default_picture.png"]) // If first image is there
    {
        // Replace the image with second image
        cellTemp.thumbnail.image = [UIImage imageNamed:@"second.png"];
    }
    else
    {
        // Replace the image with first image
        cellTemp.thumbnail.image = [UIImage imageNamed:@"default_picture.png"];
    }
}
Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
0

// Call this method in some button action so that you will get selected cars tag value
// [self getSelectedCars]; and also

@interface ViewController ()   
{   
UIImageView *thumbnailView;   
NSMutableArray *selectedThumbsArray;  
NSArray *dumyimages;  
}

- (void)viewDidLoad
{
[super viewDidLoad];
dumyimages=[[NSArray alloc]initWithObjects:[UIImage imageNamed:@"accept@2x.png"],[UIImage imageNamed:@"santa@2x.png"],[UIImage imageNamed:@"santa.png"],[UIImage imageNamed:@"accept.png"], nil];
selectedThumbsArray =[[NSMutableArray alloc]initWithObjects:@"0",@"0",@"0",@"0", nil];
int imagesCount =0;
int yAxis =50;
int xAxis =50;
for (int i=0; i<dumyimages.count; i++) {

    thumbnailView =[[UIImageView alloc]init];
    thumbnailView.frame=CGRectMake(xAxis, yAxis, 120, 120);
    thumbnailView.tag=i+10;
    thumbnailView.userInteractionEnabled=YES;
    thumbnailView.image=dumyimages[i];
    xAxis=xAxis+125;

    imagesCount=imagesCount+1;
    if (imagesCount==2) {
        xAxis=50;
        yAxis=yAxis+125;
        imagesCount=0;
    }
    [self.view addSubview:thumbnailView];
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    gestureRecognizer.delegate = self;
    gestureRecognizer.numberOfTouchesRequired = 1;
    [thumbnailView addGestureRecognizer:gestureRecognizer];
}

}
- (void)handleLongPress:(UITapGestureRecognizer*)gestureRecognizer
{
NSLog(@"Gesture tag is,%i", gestureRecognizer.view.tag-10) ;
int indexofThumb =gestureRecognizer.view.tag-10;

if ([selectedThumbsArray[indexofThumb] intValue]==0 ) {
    [selectedThumbsArray replaceObjectAtIndex:indexofThumb withObject:@"1"];

    thumbnailView=(UIImageView*)[self.view viewWithTag:indexofThumb];
    thumbnailView.image=[UIImage imageNamed:@"info.png"];
}
else
{
    [selectedThumbsArray replaceObjectAtIndex:indexofThumb withObject:@"0"];
    thumbnailView=(UIImageView*)[self.view viewWithTag:indexofThumb];
    thumbnailView.image=dumyimages[indexofThumb];
}

}
- (void)getSelectedCars
{
for (int i=0; i<selectedThumbsArray.count; i++) {

    if ([selectedThumbsArray[i] isEqualToString:@"1"]) {
        NSLog(@"indexes are ------>%d",i); // here list of index of selected images will come you can use accordingly
    }
}

}
Charan Giri
  • 1,097
  • 1
  • 9
  • 15
0

If You want to use your current code "Grid view" means

To get the indexes of image views

- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex
{
NSLog(@"%d, %d clicked", rowIndex, colIndex);
int index =(1*rowIndex)+rowIndex+colIndex; // here 1 is the max no of columns-1. 
NSLog(@"arrayindex is----->%d",index);
}
Charan Giri
  • 1,097
  • 1
  • 9
  • 15