iOS: 无法在 iCarousel 中设置图像

iOS: Can't set image in iCarousel

我在 objective-c 应用程序中使用 iCarousel。每个轮播 view 包含一个 image。所以在

-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view 

我这样设置 image

[carouselImage setImageWithURL:[NSURL URLWithString:myStringUrl] placeholderImage:[UIImage imageNamed:placeholderImage]];

这一行应用程序崩溃。这是错误:

    -[CarouselUIView setImageWithURL:placeholderImage:]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CarouselUIView setImageWithURL:placeholderImage:]: unrecognized selector sent to instance 0x123d10a70'

当我搜索时,我发现许多解决方案建议在 Build Settings 中使用 -ObjC 标志,但我已经有了这个标志。

编辑:附加代码

这是我的函数的全部代码:

-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    UIImageView * carouselImage;
    if (view == nil) {
        NSData *tempArchiveView = [NSKeyedArchiver archivedDataWithRootObject:self.carouselItemView];
        CarouselUIView * carousselView = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView];
        view = carousselView;
    }

    carouselImage = (UIImageView *) [view viewWithTag:0];


    [carouselImage setImageWithURL:[NSURL URLWithString: myStringUrl] placeholderImage:[UIImage imageNamed:imagePlaceholder]];
    return view;
}

CarouselView:

#import <UIKit/UIKit.h>

@interface CarouselUIView : UIView

@property (weak, nonatomic) IBOutlet UIImageView * carouselImage;


@end

试试这个可能对你有帮助。 如果轮播仅包含 ImageView

-(UIView *) carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    UIImageView  *imageView = (UIImageView *)view;
    if (!imageView) {
        imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth(carousel), 250)];
        imageView.tag = 100;

    }
    [imageView setImageWithURL:[NSURL URLWithString: myStringUrl] placeholderImage:[UIImage imageNamed:imagePlaceholder]];

    return imageView;
}

From looking to your given code the line you used to init the view with imageView has issue.

[view viewWithTag:0]; 

Must change tag value to other then 0. As all view's tag 属性 is set to 0 by default.

编码愉快:)