이 코드는 컬렉션보기에서 새 셀을 추가하는 데 사용됩니다. 대리자 및 데이터 소스가 올바르게 설정되었습니다. 그러나 콜렉션 뷰 셀에는 아무 것도 표시되지 않았습니다. 글쎄, 내가 그것을 디버깅 할 때 세포가 생성되었음을 보여 주지만 세포는 UIView
이외의 것을 포함하고 있는데, 그 안에 UIButton
과 UIImageView
이 포함되어 있어야한다고 기대한다. registerClass
에Objective C에서 열거 형의 숫자 값을 가져 오거나 열거 형을 숫자 값으로 설정하는 방법은 무엇입니까?
- this link :
- (void)viewDidLoad { [super viewDidLoad]; [self setImgGallery:[[NSMutableArray alloc] init]]; [[self cvPictureGallery] registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"new"]; [[self cvPictureGallery] registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"review"]; // add 5 UIImage test to imgGallery. for (int i = 0; i < 5; i++) { [[self imgGallery] addObject:[UIImage named:@"test.png"]]; } } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return MIN ([[self imgGallery] count] + 1, 5); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath]; if (cell == nil) { /* cell - contentView - button - camera icon */ cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]]; [imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)]; [imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit]; UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; [btnCamera addSubview:btnCamera]; [btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside]; [imgCameraIcon setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)]; [cell addSubview:btnCamera]; [cell setBackgroundColor:[UIColor whiteColor]]; [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"]; } return cell; } else { UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath]; if (cell == nil) { /* cell - contentView - button - image selected */ cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]]; [imgSelected setFrame:CGRectMake(0, 0, 100, 100)]; [imgSelected setContentMode:UIViewContentModeScaleAspectFill]; [imgSelected setTag:1]; UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; [btnCamera addSubview:btnCamera]; [btnCamera addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside]; [imgSelected setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)]; [cell addSubview:btnCamera]; [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"review"]; } return cell; } }
나는 이러한 품질에 대한 참조를 사용했다.
편집 : 허용 대답 거기 토론을 기반으로, 이것은 아직 아무것도 표시되지 않습니다 내 업데이트 된 코드입니다 :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath];
if (cell == nil) {
/*
cell
- contentView
- button
- camera icon
*/
cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]];
[imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)];
[imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit];
[imgCameraIcon setTag:1];
UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[btnCamera addSubview:imgCameraIcon];
[btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside];
[btnCamera setTag:2];
[imgCameraIcon setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)];
[cell addSubview:btnCamera];
[cell setBackgroundColor:[UIColor whiteColor]];
[collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"];
}
UIImageView * imgCameraIcon = (UIImageView *) [cell viewWithTag:1];
[imgCameraIcon setImage:[UIImage imageNamed:@"camera.png"]];
NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]);
return cell;
}
else {
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath];
if (cell == nil) {
/*
cell
- contentView
- button
- image selected
*/
cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]];
[imgSelected setFrame:CGRectMake(0, 0, 100, 100)];
[imgSelected setContentMode:UIViewContentModeScaleAspectFill];
[imgSelected setClipsToBounds:YES];
[imgSelected setTag:1];
UIButton * btnImage = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[btnImage addSubview:imgSelected];
[btnImage addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside];
[btnImage setTag:2];
[imgSelected setCenter:CGPointMake([btnImage width]/2, [btnImage height]/2)];
[cell addSubview:btnImage];
}
UIImageView * imgSelected = (UIImageView *) [cell viewWithTag:1];
[imgSelected setImage:[[self imgGallery] objectAtIndex:[indexPath row]]];
NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]);
return cell;
}
}
은 서브 뷰 카운트의 결과는 항상 다음은 1입니다입니다 작동하는 것이지만, 서브 뷰 카운트는 매번 증가하고 있습니다.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath];
if (cell == nil) {
/*
cell
- contentView
- button
- camera icon
*/
cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
}
UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]];
[imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)];
[imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit];
UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[btnCamera addSubview:imgCameraIcon];
[btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside];
[imgCameraIcon setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)];
[cell addSubview:btnCamera];
[cell setBackgroundColor:[UIColor whiteColor]];
[collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"];
NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]);
return cell;
}
else {
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath];
if (cell == nil) {
/*
cell
- contentView
- button
- image selected
*/
cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
}
UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]];
[imgSelected setFrame:CGRectMake(0, 0, 100, 100)];
[imgSelected setContentMode:UIViewContentModeScaleAspectFill];
[imgSelected setClipsToBounds:YES];
[imgSelected setTag:1];
UIButton * btnImage = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[btnImage addSubview:imgSelected];
[btnImage addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside];
[imgSelected setCenter:CGPointMake([btnImage width]/2, [btnImage height]/2)];
[cell addSubview:btnImage];
[collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"review"];
NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]);
return cell;
}
}
collectionview 레지스터 클래스는 상단에 있어야 참조하십시오? – Joshua
@Joshua 선택한 정답의 해결책을 수행 한 후에 올바르게 결과가 나타납니다. 그 collectionView의 수명 동안 한 번만 등록하면됩니다. –
그래,하지만 내가 선택한 셀을 제대로 재사용하지 않는다고 생각한다. – Joshua