我试图使用iCarousel库在我的应用程序中实现一个carousel类型选择器,但是当我加载应该显示carousel的视图时,什么也没有出现。
相关代码如下:
iCarousel delegate methods:
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return 65;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
// Create a number label representing a numeric WPM option
UILabel *numberLabel = nil;
// Create new view if no view is available for recycling
if (!view) {
view = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 220, 60)];
numberLabel = [[UILabel alloc] initWithFrame:view.bounds];
numberLabel.backgroundColor = [UIColor clearColor];
numberLabel.textColor = [UIColor whiteColor];
numberLabel.textAlignment = NSTextAlignmentCenter;
numberLabel.tag = 1;
[view addSubview:numberLabel];
}
else {
numberLabel = (UILabel *)[view viewWithTag:1];
}
// Calculate the number's value depending on the index value being retrieved
int number = 200 + (index * 20);
numberLabel.text = [@(number) stringValue];
return view;
}
在viewDidLoad:
中:
self.carousel = [[iCarousel alloc] initWithFrame:CGRectMake(120, 35, 150, 50)];
self.carousel.type = iCarouselTypeLinear;
[self.view addSubview:self.carousel];
我到底错过了什么,搞砸了它?
我没有使用iCarousel,但也许你没有设置委托和/或数据源?
self.carousel.delegate = self;
self.carousel.dataSource = self;
从你所展示的代码来看,它似乎不像你所做的。
以上方法为icarousel委托方法,请检查是否被调用。如果没有,检查是否在你的接口文件中添加了iCarouselDelegate, iCarouselDataSource。此外,您正在将icarousel添加为子视图。相反,它应该在nib文件或故事板本身中建立,而numberlabel应该作为carousel视图中的子视图添加。检查你是否正在做这样的事情:[carousel reloadData];这应该调用所有的carousel委托方法。
self.carousel.delegate = self;
self.carousel.dataSource = self;
[self.carousel reload];