【问题标题】:CollectionView data not showingCollectionView 数据未显示
【发布时间】:2014-10-26 07:54:07
【问题描述】:

我正在将数据发送到集合视图,但我收到错误 [__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance. 请帮助我。谢谢。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    //    NSURL *imageURL = [NSURL URLWithString:str1];
    //    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    //    UIImage *image = [UIImage imageWithData:imageData];
    //    thumbnailImgView.image = image;


    static NSString *identifier = @"Cell";

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

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img.png"]];
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"One-Album.png"]];

    return cell;

}

【问题讨论】:

  • 你能告诉我你正在填充recipeImages的代码吗?
  • - (void)viewDidLoad { [super viewDidLoad]; recipeImages = [NSArray arrayWithObjects:@"img.png", @"One-Album.png", @"Create-Album.png", @"AllPhotos.png", @"Albums.png", @"brightness1.png ", @"original1 (1).png", @"crop1.png", nil]; // 加载视图后做任何额外的设置。 }
  • - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return [recipeImages count]; }
  • 请编辑问题。
  • 我在这一行遇到错误。 recipeImageView.image = [UIImage imageNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]];

标签: ios objective-c iphone ios7 uicollectionview


【解决方案1】:

这行好像错了[recipeImages[indexPath.section] objectAtIndex:indexPath.row]

如果recipeImagesNSArrayNSMutableArray 的类型,则应使用[recipeImages objectAtIndex:indexPath.row]

示例:

if([recipeImages count] > indexPath.row)
{
   recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
}
else
{
  NSLog(@"Unable to get the image name");
}

【讨论】:

  • -[CustomCell setImage:]: unrecognized selector sent to instance 这是我使用此代码时遇到的错误
  • 您是否添加了支票? if([recipeImages count] > indexPath.row) ,请参阅更新后的答案。
【解决方案2】:

这里的问题是您试图将NSString 视为NSArray!当你说, recipeImages[indexPath.section],它会返回一个字符串类型。现在,如果您尝试在 String 类型上执行 objectAtIndex,它将崩溃。

[recipeImages objectAtIndex:indexPath.row] 应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2021-03-08
    • 2020-02-04
    • 2021-05-04
    • 1970-01-01
    相关资源
    最近更新 更多