ios - 如何在 Objective C 中获取枚举的数值,或用数值设置枚举?

标签 ios objective-c uicollectionview uicollectionviewcell

我有这段代码可以在 Collection View 上添加新单元格。委托(delegate)和数据源已正确设置。但是 Collection View 单元格没有显示任何内容。好吧,当我调试它时,它显示单元格已创建,但单元格只包含一个 UIView,我希望它应该包含一个 UIButton 和一个 UIImageView 在里面。

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setImgGallery:[[NSMutableArray alloc] init]];
    [[self cvPictureGallery] registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"new"];
    [[self cvPictureGallery] registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"review"];

    // add 5 UIImage test to imgGallery.
    for (int i = 0; i < 5; i++) {
        [[self imgGallery] addObject:[UIImage named:@"test.png"]];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return MIN ([[self imgGallery] count] + 1, 5);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon
        UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath];

        if (cell == nil) {
            /*
             cell
             - contentView
               - button
                 - camera icon
            */

            cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

            UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]];
            [imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)];
            [imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit];

            UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
            [btnCamera addSubview:btnCamera];
            [btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside];
            [imgCameraIcon setCenter:CGPointMake([btnCamera width] / 2, [btnCamera height] / 2)];

            [cell addSubview:btnCamera];
            [cell setBackgroundColor:[UIColor whiteColor]];
            [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"];
        }
        return cell;
    }
    else {
        UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath];
        if (cell == nil) {
            /*
             cell
             - contentView
               - button
                 - image selected
            */

            cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

            UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]];
            [imgSelected setFrame:CGRectMake(0, 0, 100, 100)];
            [imgSelected setContentMode:UIViewContentModeScaleAspectFill];
            [imgSelected setTag:1];

            UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
            [btnCamera addSubview:btnCamera];
            [btnCamera addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside];
            [imgSelected setCenter:CGPointMake([btnCamera width] / 2, [btnCamera height] / 2)];

            [cell addSubview:btnCamera];
            [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"review"];
        }
        return cell;
    }
}

我在这些 QA 上使用了引用:

编辑:根据那里接受的答案和讨论,这是我更新后的代码,但仍然没有显示任何内容:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon
        UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath];

        if (cell == nil) {
            /*
             cell
             - contentView
               - button
                 - camera icon
            */

            cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

            UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]];
            [imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)];
            [imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit];
            [imgCameraIcon setTag:1];

            UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
            [btnCamera addSubview:imgCameraIcon];
            [btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside];
            [btnCamera setTag:2];
            [imgCameraIcon setCenter:CGPointMake([btnCamera width] / 2, [btnCamera height] / 2)];

            [cell addSubview:btnCamera];
            [cell setBackgroundColor:[UIColor whiteColor]];
            [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"];
        }

        UIImageView * imgCameraIcon = (UIImageView *) [cell viewWithTag:1];
        [imgCameraIcon setImage:[UIImage imageNamed:@"camera.png"]];

        NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]);

        return cell;
    }
    else {
        UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath];
        if (cell == nil) {
            /*
             cell
             - contentView
               - button
                 - image selected
            */

            cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

            UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]];
            [imgSelected setFrame:CGRectMake(0, 0, 100, 100)];
            [imgSelected setContentMode:UIViewContentModeScaleAspectFill];
            [imgSelected setClipsToBounds:YES];
            [imgSelected setTag:1];

            UIButton * btnImage = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
            [btnImage addSubview:imgSelected];
            [btnImage addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside];
            [btnImage setTag:2];
            [imgSelected setCenter:CGPointMake([btnImage width] / 2, [btnImage height] / 2)];

            [cell addSubview:btnImage];
        }

        UIImageView * imgSelected = (UIImageView *) [cell viewWithTag:1];
        [imgSelected setImage:[[self imgGallery] objectAtIndex:[indexPath row]]];

        NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]);

        return cell;
    }
}

subview 计数的结果始终为 1。下面是工作结果,但 subview 计数每次都在增加。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon
        UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath];

        if (cell == nil) {
            /*
             cell
             - contentView
               - button
                 - camera icon
            */

            cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        }

        UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]];
        [imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)];
        [imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit];

        UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [btnCamera addSubview:imgCameraIcon];
        [btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside];
        [imgCameraIcon setCenter:CGPointMake([btnCamera width] / 2, [btnCamera height] / 2)];

        [cell addSubview:btnCamera];
        [cell setBackgroundColor:[UIColor whiteColor]];
        [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"];

        NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]);

        return cell;
    }
    else {
        UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath];
        if (cell == nil) {
            /*
             cell
             - contentView
               - button
                 - image selected
            */

            cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        }

        UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]];
        [imgSelected setFrame:CGRectMake(0, 0, 100, 100)];
        [imgSelected setContentMode:UIViewContentModeScaleAspectFill];
        [imgSelected setClipsToBounds:YES];
        [imgSelected setTag:1];

        UIButton * btnImage = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [btnImage addSubview:imgSelected];
        [btnImage addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside];
        [imgSelected setCenter:CGPointMake([btnImage width] / 2, [btnImage height] / 2)];

        [cell addSubview:btnImage];
        [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"review"];

        NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]);

        return cell;
    }
}

最佳答案

我认为您编写了错误的代码来创建单元格。请像下面这样更新您的代码并查看输出

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon
        UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath];

        if (cell == nil) {
            /*
             cell
             - contentView
             - button
             - camera icon
             */

            cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        }
        UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]];
        [imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)];
        [imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit];

        UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [btnCamera addSubview:btnCamera];
        [btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside];
        [imgCameraIcon setCenter:CGPointMake([btnCamera width] / 2, [btnCamera height] / 2)];

        [cell addSubview:btnCamera];
        [cell setBackgroundColor:[UIColor whiteColor]];
        [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"];

        return cell;
    }
    else {
        UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath];
        if (cell == nil) {
            /*
             cell
             - contentView
             - button
             - image selected
             */

            cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        }
        UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]];
        [imgSelected setFrame:CGRectMake(0, 0, 100, 100)];
        [imgSelected setContentMode:UIViewContentModeScaleAspectFill];
        [imgSelected setTag:1];

        UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [btnCamera addSubview:btnCamera];
        [btnCamera addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside];
        [imgSelected setCenter:CGPointMake([btnCamera width] / 2, [btnCamera height] / 2)];

        [cell addSubview:btnCamera];
        [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"review"];
        return cell;
    }
}

关于ios - 如何在 Objective C 中获取枚举的数值,或用数值设置枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42110786/

相关文章:

ios - 使用 Twilio iOS sdk 和构建 Cordova 应用程序时 openssl 崩溃

ios - 可以通过 Storyboard属性检查器引用标准 iOS UIBarButtonSystemItem 图标吗​​?

iphone - 我应该在哪里存储图像,以便我可以使用 iTunes 文件共享它们?

ios - 单击 tableview 中的 collectionview 单元格导航

ios - 使用可区分数据源时如何处理空状态 - UICollectionViewDiffableDataSource?

iOS 10 (Swift) 拒绝通知权限

ios - Opengles片段着色器实现效果

objective-c - NSMutableArray:添加和提取结构

objective-c - 如何抑制来自 Xcode 项目的头文件警告

ios - 如何实现ViewController里面有CollectionView?