OGeek|极客世界-中国程序员成长平台

标题: ios - 在uigridview中的行和列设置图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:50
标题: ios - 在uigridview中的行和列设置图像

我正在使用 UIGridView,因为 GridView 将显示在 View 中,有 2 列等等......我只是使用 UIImageView 以便我的图像设置在 rowindex 的每一行和 index 的列上

这是我的代码:

 - (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) gridViewUIGridView *)grid widthForColumnAtint)columnIndex
{
    return 160;
}

- (CGFloat) gridViewUIGridView *)grid heightForRowAtint)rowIndex
{
    return 200;
}

- (NSInteger) numberOfColumnsOfGridViewUIGridView *) grid
{
    return 2;
}


- (NSInteger) numberOfCellsOfGridViewUIGridView *) grid
{
    return [CarArray count];
}

- (UIGridViewCell *) gridViewUIGridView *)grid cellForRowAtint)rowIndex AndColumnAtint)columnIndex
{
    Cell *cell = (Cell *)[grid dequeueReusableCell];

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

    cell.thumbnail.image=[UIImage imageNamed:[CarArray objectAtIndex:rowIndex && columnIndex]];

但是我的图像设置不正确。image.png 显示在行中,column(0,0)(0,1)(1,0)..并且 car-image.png 出现在 (1,1 )。

请帮忙 提前致谢。



Best Answer-推荐答案


cellForRowAtint)rowIndex 中将 [CarArray objectAtIndex:rowIndex && columnIndex] 更改为 [CarArray objectAtIndex:(rowIndex*2+columnIndex)] AndColumnAt:(int)columnIndex 方法。

&& 是逻辑与运算符,0&&0, 0&&1,1&&0 是 0。而 1&&1 是 1。这就是为什么你有奇怪的图像显示。你需要的是 rowIndex*(columnsPerRow)+columnIndex 这里。

关于ios - 在uigridview中的行和列设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20898666/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4