1

Я новичок в UICollectionView.Ячейка UICollectionView не добавляется как подзаголовок UICollectionView

Я следовал this tutorial, чтобы узнать, как использовать UICollectionView, но проблема заключается в том, что в .xib (не используя раскадровки) Я не могу добавить UICollectionViewCell как подвид из UICollectionView.

В учебнике ячейка автоматически создается под UICollectionView в .xib.

Любая помощь будет оценена по достоинству.

+0

Что вы пытались добавить UICollectionViewCell в качестве подзаголовка? –

+0

Нам нужен ваш код, пожалуйста, Мы не можем догадаться, что происходит. –

+0

В противном случае, как я могу добавить элементы? – Jeff

ответ

3

Это учебник для работы в Строй борту в XIb вы ве создать пользовательские ячейки сначала создать вид контроллера добавить UicollectionView для просмотра Controller

enter image description here enter image description here

затем создать пользовательский класс для сбора ячейки

enter image description here

затем craete пустой XIb ресурс и добавить вид коллекции ячейки enter image description here enter image description here enter image description here

и установить имя класса и идентификатор для этого элемента управления enter image description here

затем создать делегаты метод добавления пользовательская ячейка к ur collectionViewController

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellIdentifier = @"customCell"; 

    customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

    return cell; 

} 
+0

: Благодарим вас за ответ. Но afetr с помощью кода ur код приложения получает сбой с ошибкой *** Ошибка утверждения в - [UICollectionView _dequeueReusableViewOfKind: withIdentifier: forIndexPath:] Я решил это с помощью кода [self.CollectionView registerClass: [PeopleHub class] forCellWithReuseIdentifier: @ "Cell"] в viewDidload.but после этого также я запускаю нулевое значение для изображения, используемого в ячейке PeopleHub * cell = [collectionView dequeueReusableCellWithReuseIdentifier: cellIdentifier forIndexPath: indexPath]; NSLog (@ "cell image% @", cell.imgAvatar); .. – Jeff

+0

вы можете показать код @Jeff – Yohan

+0

Спасибо Yohan. Исправлена ​​ошибка, заменив [self.cv registerClass ....] на [self. cv регистраNib ....] – Jeff

0

Вы можете начать свой контроллер и создать вид ячейки пользовательской коллекции и для настройки вида элемента, а затем вы можете использовать его в коллекции контроллер представления в XIb, здесь хорошие учебники, чтобы сделать это http://adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial/

0

Используйте этот код для создания коллекции и добавления подсмотра.

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 

    return [imageArray count]; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 

    NSMutableArray *sectionArray = [imageArray objectAtIndex:section]; 
    return [sectionArray count]; 

} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    // Setup cell identifier 
    static NSString *cellIdentifier = @"cvCell"; 

    /* Uncomment this block to use nib-based cells */ 
    // UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    // UILabel *titleLabel = (UILabel *)[cell viewWithTag:100]; 
    // [titleLabel setText:cellData]; 
    /* end of nib-based cell block */ 

    /* Uncomment this block to use subclass-based cells */ 
    CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    NSMutableArray *data = [imageArray objectAtIndex:indexPath.section]; 
    NSString *cellData = [data objectAtIndex:indexPath.row]; 
    [cell.titleLabel setText:cellData]; 

    cell.imageView.image = [UIImage imageNamed:[imageArray[indexPath.section] objectAtIndex:indexPath.row]]; 

    return cell; 

} 
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{ 
    header *sectionHeader; 
    if (kind == UICollectionElementKindSectionHeader) 
    { 
     sectionHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Hello" forIndexPath:indexPath]; 
    } 
    UICollectionReusableView *test=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Hello" forIndexPath:indexPath]; 
    return test; 

} 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section 
{ 
    if (section == 3) { 
     return CGSizeMake(0, 100); 
    } 

    return CGSizeZero; 
} 


- (NSString *)applicationDocumentsDirectory { 
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
} 


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(80, 80); 
} 
Смежные вопросы