[更新-出于某种原因集合视图为空...]
出于某种原因,根本没有调用任何集合视图委托方法(cellforitemat,numberofitemsinsection,sizeforitem等)。我已经设置了数据源和委托。我已经在界面生成器中尝试过,并且在视图中的代码中确实加载了它。我想念什么吗?
在.h中:
@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSArray *workoutList;
.m中(在VDL中,printout语句上的workoutlist.count为3,但未在其他位置运行):
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.workoutCV.backgroundColor = [UIColor blackColor];
self.view.backgroundColor = [UIColor blackColor];
self.workoutCV.delegate = self;
self.workoutCV.dataSource = self;
NSLog(@"here %lu", (unsigned long)self.workoutList.count);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
NSLog(@"and here %lu", (unsigned long)self.workoutList.count);
return self.workoutList.count;
}
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString * const cellId = @"HomeCell";
HomeMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(CGRectGetWidth(collectionView.frame), CGRectGetHeight(collectionView.frame) / self.workoutList.count);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
我是从另一个Viewcontroller(HomeController)切换到这个新控制器的。这是HomeController .m中的代码:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
ViewController *nextVC = [[ViewController alloc] init];
nextVC.workoutList = @[@"Squats", @"Stiff Leg DL", @"Abs"];
[self.navigationController pushViewController:nextVC animated:true];
}
它推动下一个VC正常运行,并运行print语句,但是没有显示任何单元格,也没有从任何集合视图委托方法运行print语句