CollectionView数据未显示

时间:2014-09-02 06:01:15

标签: ios objective-c iphone ios7 uicollectionview

我正在将数据发送到集合视图但我收到错误[__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;

}

2 个答案:

答案 0 :(得分:0)

这一行似乎有误[recipeImages[indexPath.section] objectAtIndex:indexPath.row]

如果recipeImages属于NSArrayNSMutableArray,则应使用[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");
}

答案 1 :(得分:0)

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

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