2017-06-29 147 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