UICollectionView 나타나지 않는
-
11-12-2019 - |
문제
나가려고 설정 UICollectionView
프로그래밍 내보기 컨트롤러는 것이다: UIViewController
.어떤 이유로,내 컬렉션 보기를 표시하지 않습니다.아래가 할 수 있습니다.
왜 그것이 나타나지 않는?나는 그것을 연결을 대리하고 데이터 원본을 추가로 서브뷰를 self.view
.무엇이 내 코드는?
나 .h
파일:
@interface MainViewController : UIViewController
{
@private
UICollectionView *_collectionView;
NSMutableArray *_results; // data source array
}
@end
나 .m
파일:
@interface MainViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *results;
@end
@implementation MainViewController
@synthesize collectionView = _collectionView;
@synthesize results = _results;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// some init stuff - nothing to do with collection view.
}
return self;
}
- (void)loadView
{
self.results = [NSMutableArray array];
UIImage *image1 = [UIImage imageNamed:@"img1.jpg"];
UIImage *image2 = [UIImage imageNamed:@"img2.jpg"];
[self.results addObject:image1];
[self.results addObject:image2];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
self.collectionView = collectionView;
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
[self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return [self.results count];
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
UIImage *image = [self.results objectAtIndex:indexPath.row];
return CGSizeMake(image.size.width, image.size.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
해결책
나는 오류를 시도하는 코드를 실행하지 않는 한 나는 변경 ib 유 방법을 viewDidLoad--에 따라 문서 당신은 가정하지 않은 직접 전화 ib 유.한 데이터를 얻을 수원 및 대리인 방법을 실행에 옮겼 라인을 설정 대리인 및 데이터 소스를 자기 아래는 당신이 설정한 자.collectionView=collectionView
self.collectionView = collectionView;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
다른 팁
귀하의 numberOfSectionsInCollectionView:
환 0
.컬렉션에 대해보기로 한 섹션 중 하나를 수행해야 합니다 돌아 1
또는 이 메소드를 구현하지 않.
도 난 볼 수 없습니다 당신이 유지하거나 복사 self.collectionView
.
나는 하위 클래스 UICollectionViewController 신 UIViewController
과 변경하면 초기화 방법을:
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
고 일했다.
당신만을 바인딩 CollectionView 대리인 및 데이터 소스를 ViewController 에서는 c++코드가 필요합니다.입력한 이미지는 여기에 설명