uicollectionviewcontroller ios objective-c uicollectionview frame

ios - uicollectionviewcontroller - uicollectionviewcell indexpath



Cambiar la posiciĆ³n de la etiqueta en UICollectionViewCell (4)

Autolayout anulará sus llamadas a setFrame: Puede agregar algunos puntos de venta en su celda con las restricciones de la etiqueta y cambiarlos para cambiar el marco.

Tenemos un UICollectionView que tiene un prototipo de celda en el Storyboard. La celda tiene una UILabel (" label ") en ella, que se coloca sin autolayout.

Establecemos el marco de la etiqueta condicionalmente en -collectionView:cellForItemAtIndexPath: con setFrame como este:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CategoryCell *cell; cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category" forIndexPath:indexPath]; ... if (self.isViewTypeList) { [((CategoryCell *)cell).label setFrame:FRAME_RECT]; } ... return cell; }

Lo hemos inspeccionado, y parece que antes que nada llama a la etiqueta -setFrame: con los valores de Storyboard cuando llega el dequeing, nuestro código sigue, y esto establece los nuevos fotogramas correctamente. No hay otra llamada más que estas, pero la etiqueta permanece en su posición original en la pantalla.

¿Qué pasa? ¿Por qué el nuevo marco no está configurado correctamente? ¿Hay algo más que lo sobrediga? ¿Debo retransmitir o actualizar de alguna manera?


Por favor, intente seguir los pasos. Esto funcionará para ti-

  • En su guión gráfico o archivo XIB, seleccione la celda del prototipo
  • Seleccione la etiqueta para la cual desea cambiar el marco
  • Establezca la propiedad de la tag en Attributes Inspector (View Section) , como set tag = 101

Después de eso, en tu sección de códigos haz esto:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { //... CategoryCell *cell; cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category" forIndexPath:indexPath]; //... if (self.isViewTypeList) { UILabel *lbl = (UILabel *)[cell viewWithTag:101]; [lbl setFrame:FRAME_RECT]; } //... }

¡Buena suerte!


tratar:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { //... //instead of //CategoryCell *cell; //cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category" // forIndexPath:indexPath]; CategoryCell *cell = (CategoryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Category" forIndexPath:indexPath]; //sure reuse-identifier is "Category"? //... if (self.isViewTypeList) { //instead of //[((CategoryCell *)cell).label setFrame:FRAME_RECT]; [cell.label setFrame:FRAME_RECT]; } //... return cell; }

también vaya a través de este enlace y vea si le faltan algunos pasos cruciales:
http://ashfurrow.com/blog/uicollectionview-example


Desmarque Autolayout de xib. Puede ayudar a cambiar el marco.