2014-04-16 3 views
0

Я пытаюсь что-то сделать с UICollectionView. Я хочу, чтобы они сосредоточены в центре, но для этого мне нужно создать что-то вроде этого:UICollectionview Sections/Rows/Array

(Array consists of 5 items) 
X X <- 2 items in section 1 
X X <- 2 items in section 2 
X <- 1 item in section 3 

Но проблема в том, что элементы не отображаются правильно? Если бы у меня было 3 предмета. Элементы будут такими, как в разделе 1, пункт 1 и 2. а затем в разделе 2 снова будет пункт 1, вместо этого, если элемент 3 моего массива.

Это мой код:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    float uitkomst = ceilf((float)[arrayDobbel count]/2) - 1; // Array starts at 0 so need to do - 1 

    if (uitkomst == section) { 
     return 1; // Last cell 
    } 
    return 2; //Always 2 cells to each other, not the last section 
} 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 
    float uitkomst = ceilf((float)[arrayDobbel count]/2); //Calculate how much sections. Always 2 items/section 

    return (int)uitkomst; 
} 

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

    detailDice *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dobbelsteenGame" forIndexPath:indexPath]; 

    cell.layer.cornerRadius = 25; 
    cell.layer.backgroundColor = [[UIColor colorWithRed:0.188 green:0.341 blue:0.612 alpha:1.0] CGColor]; 
    [[cell detaildiceLabel] setText: [NSString stringWithFormat:@"%@", [[arrayDobbel objectAtIndex:indexPath.row] returnRandomOptie]]]; 

    return cell; 
} 

Может кто-нибудь помочь мне с показом правильных текстов в нужных клетках?

С наилучшими пожеланиями

+0

Вы играете с 'section' и' 'row' и section'isn't, используемого в вашем' CollectionView: cellForItemAtIndexPath: ' – Larme

ответ

2

В cellForItemAtIndexPath:

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

    .... 
    //since there is 2 items in each section 
    int index = 2*indexPath.section + indexPath.row; 
    [[cell detaildiceLabel] setText: [NSString stringWithFormat:@"%@", [[arrayDobbel objectAtIndex:index] returnRandomOptie]]]; 

    return cell; 
} 
+0

Спасибо человек! Это было быстро! – Kets

+0

@Kets Добро пожаловать – Akhilrajtr

Смежные вопросы