uitableviewcell tutorial create collection ios uicollectionview uicollectionviewcell

ios - tutorial - Especifique el número de fila y columna para UICollectionView



uicollectionview in uitableviewcell (5)

El enfoque más general para lograr n columnas en la vista de colección que sea compatible en cualquier orientación es

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { // flow layout have all the important info like spacing, inset of collection view cell, fetch it to find out the attributes specified in xib file guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else { return CGSize() } // subtract section left/ right insets mentioned in xib view let widthAvailbleForAllItems = (collectionView.frame.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right) // Suppose we have to create nColunmns // widthForOneItem achieved by sunbtracting item spacing if any let widthForOneItem = widthAvailbleForAllItems / nColumns - flowLayout.minimumInteritemSpacing // here height is mentioned in xib file or storyboard return CGSize(width: CGFloat(widthForOneItem), height: (flowLayout.itemSize.height)) }

Quiero mostrar un UICollectionView que contiene exactamente 2 filas y 100 celdas en cada fila.

// 1 - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { return 100; } // 2 - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { return 2; } // 3 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.lbl.text = @"Data"; return cell; }

Estoy recibiendo esto:

¿Cómo puedo especificar un número de fila para UICollectionView? Quiero crear una tabla de 2x100.


El número de secciones determina cuántas celdas habrá en cada fila, asumiendo que el tamaño de la celda lo permita.

Una sección siempre comenzará una nueva fila.


Para 3 celdas por fila .. Agregar este código.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(collectionView.frame.size.width/3.2 , 100); }


Prueba esto:

// 1 - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { return 2; } // 2 - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { return 100; } // 3 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.lbl.text = @"Data"; return cell; }


Para Swift : 3 filas por columna (utilizando la respuesta de @SakhshiSingla)

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 3 } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSize(width: collectionView.frame.size.width/3.2, height: 100) }