【问题标题】:Should I Add Something To The Header File?我应该在头文件中添加一些东西吗?
【发布时间】:2013-07-25 16:00:51
【问题描述】:

我正在编写一个应用程序并收到此错误。不知道我将如何修复它。谢谢。

错误是未声明的标识符“collectionView”

并且正在导入头文件。

标题:

#import <UIKit/UIKit.h>
NSMutableData *content;

@interface AMCollectionViewController : UICollectionViewController


@property (weak, nonatomic) IBOutlet UIBarButtonItem *tagButton;

- (IBAction)tagButtonTouched:(id)sender;

@end

发生错误时的实现

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
    if(shareEnabled){
        //determine the selected items by using the indexPath
        NSString *selectedPhotos = [tagImages[indexPath.section] objectAtIndex:indexPath];

        //add selected image into the array
        [selectedPhotos addObject:selectedPhotos];
    }
}

非常感谢大家

我在同一个 .m 文件中多次使用 collectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 50;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *identifier =@"Cell";

        AMCollectionViewCell *cell = (AMCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

        int imageNumber = indexPath.row % 10;



        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"imageicon%d.jpg",imageNumber]];
        cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageBorder.jpg"]];


        return cell;
    }

【问题讨论】:

  • 您是否在实现 (.m) 文件中 #importing 您的标头 (.h)?
  • 请使用edit在问题中添加错误
  • 您有一个名为 collectionView 的东西,它尚未在任何地方声明。你觉得你如何声明和初始化collectionView?

标签: objective-c compiler-errors uicollectionview


【解决方案1】:

我不知道那个错误,但是

  1. 您在此行中对indexPath 的第二次引用不正确:

    NSString *selectedPhotos = [tagImages[indexPath.section] objectAtIndex:indexPath];
    

    objectAtIndex 参数需要一个整数。您可能的意思是:

    NSString *selectedPhotos = [tagImages[indexPath.section] objectAtIndex:indexPath.item];
    

    或者,为了更加一致(使用objectAtIndex 或使用[] 语法,但同时使用这两者有点奇怪):

    NSString *selectedPhotos = tagImages[indexPath.section][indexPath.item];
    
  2. 你没有显示tagImages的声明,也没有初始化,但我假设它是tagImages一个字符串数组?如果是这样,上述更正应该可以解决它。如果没有,您应该告诉我们tabImages 是如何填充/结构化的。

  3. 如果selectedPhotos 是NSString,则addObject 方法的使用可能不起作用。 addObject 方法是 NSMutableArray 方法,而不是 NSString 方法。

【讨论】:

  • 我实现了您建议的所有更改,我唯一的错误仍然是未声明的标识符“collectionView”
  • @user2469203 在哪一行代码向您显示了该错误?也许您可以使用当前代码更新您的问题,并明确指出哪一行代码正在生成该错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
相关资源
最近更新 更多