2

i am using UIGridView as GridView will be shown in view,Having 2 column and so on ......i m just using and UIImageView so that my image is set on each row at rowindex and column at index

Here is my Code:

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    
//    img_ary=[[NSArray alloc]init];
//    img_ary= [NSArray arrayWithObjects:@"car-image.png",@"slider-image.png",@"car-image.png",@"slider-image.png",@"car-image.png",@"slider-image.png", nil];
//    [self collectioncreate];

    CarArray = [[NSMutableArray alloc]init];
    [CarArray addObject:@"image.png"];
      [CarArray addObject:@"car-image.png"];
    [CarArray addObject:@"car-thumnail.png"];
    [CarArray addObject:@"image.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 && columnIndex]];

But my image is not set properly.image.png is shown in row,column(0,0)(0,1)(1,0)..and car-image.png is occured on (1,1).

Please Help Thanks in advance.

1 Answer 1

1

change [CarArray objectAtIndex:rowIndex && columnIndex] to [CarArray objectAtIndex:(rowIndex*2+columnIndex)] in your cellForRowAt:(int)rowIndex AndColumnAt:(int)columnIndex method.

&& is Logical AND Operator, 0&&0, 0&&1,1&&0, are 0. and 1&&1 is 1. that's why you have weird images display. What you need is rowIndex*(columnsPerRow)+columnIndex here.

11
  • @sir do u know how to put them all value as mark or unmark if i want to delete any row column?
    – JohnWick
    Jan 3, 2014 at 7:45
  • sir and what is (rowIndex*2+columnIndex) can u explain?
    – JohnWick
    Jan 3, 2014 at 7:46
  • rowIndex*(columnsPerRow)+columnIndex,2 is column number per row.
    – chancyWu
    Jan 3, 2014 at 7:47
  • and if i want to select row/column with checkmark then ? any idea?sir
    – JohnWick
    Jan 3, 2014 at 7:51
  • you need a 2 dimension array or 1 dimension array to remember the check status. then check it in your cellForRowAt method.
    – chancyWu
    Jan 3, 2014 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.