2017-06-29 25 views
0

次の実装では、何を試しても、カルーセルに画像が表示されることはありません。代理人とデータソースを設定しました。画像をダウンロードしながら、あなたはカルーセルをリロードする必要がカルーセルが動作しない

ヘッダー

#import "UIImageView+AFNetworking.h" 
#import <AFNetworking/AFNetworking.h> 
#import "iCarousel.h" 

@interface GalleryViewController : UIViewController<iCarouselDataSource, iCarouselDelegate> 
@property (strong, nonatomic) UIImageView *galleryImageView; 
@property (nonatomic, strong) NSMutableArray *gElements; 
@property (nonatomic, strong) iCarousel *carousel; 


@end 

実装

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.gElements = [[NSMutableArray alloc] init]; 
    self.carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; 
    self.carousel.delegate = self; 
    self.carousel.dataSource = self; 
    self.carousel.type = iCarouselTypeCylinder; 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^(void) { 
     [self loadGalleryImages]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.carousel reloadData]; 
     }); 
    }); 
} 

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel 
{ 
    return [gElements count]; 
} 

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

    self.galleryImageView = nil; 
    //create new view if no view is available for recycling 
    if (carouselView == nil) 
    { 
     carouselView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; 
     self.galleryImageView = [[UIImageView alloc]initWithFrame:carouselView.frame]; 
     self.galleryImageView.tag = 0; 
     [carouselView addSubview:self.galleryImageView]; 
    } 
    else 
    { 
     self.galleryImageView = (UIImageView*)[carouselView viewWithTag:0]; 
    } 

    if([gElements count] >0) 
    { 
    // gElement has 10 NSURL objects 
     NSURLRequest *request = [NSURLRequest requestWithURL:[gElements objectAtIndex:index]]; 

     NSLog(@ "index %ld", (long)index); 
     __weak typeof(self) weakSelf = self; 

     [galleryImageView setImageWithURLRequest:request 
         placeholderImage:nil 
           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 

          weakSelf.galleryImageView.image = image;           
     } 
     failure:nil]; 
    } 
    return carouselView; 
} 

答えて

1

注:比較して、あなたのコードに置き換え

-(UIView *) carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{ 
    UIView *globleView = (UIView *)view; 
    if (!globleView) { 
     globleView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth(carousel), 250)]; 
     globleView.backgroundColor = [UIColor clearColor]; 
     UIImageView *imageView = [[UIImageView alloc]init]; 
     imageView.image = [UtilityClass placeholder]; 
     imageView.contentMode = UIViewContentModeScaleAspectFit; 
     imageView.tag = 100; 
     [globleView addSubview:imageView]; 

     if (index == 0) { 
      [self refreshCarousleViewWithView:globleView withIndex:index]; 
     } 
    } 
    return globleView; 
} 



- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{ 
} 

- (BOOL)carousel:(iCarousel *)carousel shouldSelectItemAtIndex:(NSInteger)index{ 
    return YES; 
} 

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel{ 
    [self refreshCarousleViewWithView:carousel.currentItemView withIndex:carousel.currentItemIndex]; // 
} 

-(void)refreshCarousleViewWithView:(UIView *)currentView withIndex:(NSInteger)index{ 
    if([gElements count] >0) 
    { 
    // gElement has 10 NSURL objects 
     NSURLRequest *request = [NSURLRequest requestWithURL:[gElements objectAtIndex:index]]; 

     NSLog(@ "index %ld", (long)index); 
     UIImageView *galleryImageView = (UIImageView *)[view viewWithTag:100];  
     [galleryImageView setImageWithURLRequest:request 
         placeholderImage:nil 
           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 

          galleryImageView.image = image;           
     } 
     failure:nil]; 
    } 

} 

OR

あなたが見ることカルーセルを追加するのを忘れています。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.gElements = [[NSMutableArray alloc] init]; 
    self.carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; 
    self.carousel.delegate = self; 
    self.carousel.dataSource = self; 
    self.carousel.type = iCarouselTypeCylinder; 
[self.view addSubView:self.carousel]; // add this line 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^(void) { 
     [self loadGalleryImages]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.carousel reloadData]; 
     }); 
    }); 
} 
+0

動作しません。 – hotspring

+0

私は私のプロジェクトで働いているブロック全体を投稿します。 – KKRocks

+0

ハードコードされた単一のイメージでテストしましたが、まだ表示されません。 – hotspring

0

あなたが逃したものはわかりませんが、私のコードは動作しています。

class FindGigCardViewVC: BaseVC,iCarouselDataSource, iCarouselDelegate { 

    @IBOutlet var carousel: iCarousel! 
    var arrGigData = NSMutableArray() 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     carousel.type = .linear 
    } 

    func reloadDataInCarousel() 
    { 
     carousel.reloadData() 
    } 

    // MARK: - carousel delegate 

    func numberOfItems(in carousel: iCarousel) -> Int { 
     return arrGigData.count 
    } 

    func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView 
    { 
     return your view 
    } 

    func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat { 
     if (option == .spacing) { 
       return value * 1.1 
     } 
     return value 
     } 

    func carouselCurrentItemIndexDidChange(_ carousel: iCarousel) 
    { 
     print(carousel.currentItemIndex) 
    } 
} 
+0

ハードコードされた単一のイメージでもテストしましたが、まだ表示されません。 – hotspring

+0

イメージのダウンロードで問題が発生している場合は、このライブラリhttps://github.com/rs/SDWebImageを使用することをお勧めします。 – Parimal

関連する問題