我正在使用内置UITabbar的视图控制器并插入一个具有CHTCollectionViewWaterfallLayout的子视图,但我收到了运行时错误:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UICollectionView的委托应符合CHTCollectionViewDelegateWaterfallLayout协议'
我已经宣布 CHTCollectionViewDelegateWaterfallLayout 并实施它所需的方法
如何解决它,任何解决方案?
我的第一个视图控制器代码:
- (void)viewDidLoad {
[super viewDidLoad];
[self.myTabbar setAlpha:0.95];
self.myTabbar.delegate = self;
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
[self.view insertSubview:[storyBoard instantiateViewControllerWithIdentifier:@"tab1"].view belowSubview:self.myTabbar];
[self.myTabbar setSelectedItem:self.myTabbar.items[0]];
self.currentItem = [self.myTabbar selectedItem];}
这里是具有集合视图.h文件的视图控制器:
#import <UIKit/UIKit.h>
#import "CHTCollectionViewWaterfallLayout.h"
@interface MainViewController : UIViewController <UICollectionViewDataSource, CHTCollectionViewDelegateWaterfallLayout, UICollectionViewDelegate, UICollectionViewDataSourcePrefetching>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionViewController;
@end
和.m文件中委托的必需方法:
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionViewController.prefetchDataSource = self;
self.collectionViewController.dataSource = self;
self.collectionViewController.delegate = self;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item % 2 == 1) {
CGFloat cellWidth = [UIScreen mainScreen].bounds.size.width /2.2;
CGFloat cellHeight = cellWidth * 1.6;
return CGSizeMake(cellWidth, cellHeight);
} else {
CGFloat cellWidth = [UIScreen mainScreen].bounds.size.width /2.2;
CGFloat cellHeight = cellWidth * 2;
return CGSizeMake(cellWidth, cellHeight);
}
}
答案 0 :(得分:0)
请从<UICollectionViewDelegate>
删除MainViewController
的注册信息。您的MainViewController
界面应该如下所示 -
#import <UIKit/UIKit.h>
#import "CHTCollectionViewWaterfallLayout.h"
@interface MainViewController : UIViewController <UICollectionViewDataSource, CHTCollectionViewDelegateWaterfallLayout, UICollectionViewDataSourcePrefetching>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionViewController;
@end