【问题标题】:Set text in uicollectionviewcell在 uicollectionviewcell 中设置文本
【发布时间】:2014-11-21 10:35:31
【问题描述】:

我创建了一个 UIcollectionView 和一个数组,其中包含一些字符串 @[@"Item One", "Item Two", @"Item Three"];

在 Tableview 中我会这样做:

NSString *object = self.titlesArray[indexPath.row];
cell.textLabel.text = object;

但我真的不知道如何为 UIcollectionView 中的项目执行此操作。

【问题讨论】:

标签: ios objective-c


【解决方案1】:

UICollectionViewCell 没有默认的单元格样式。 你必须创建一个自定义的UICollectionViewCell 并在里面添加一个UILabel

【讨论】:

    【解决方案2】:

    UICollectionViewCell 没有UITableviewCell 的默认textLabel,您必须根据需要创建自定义UICollectionViewCell

    您可以查看this 教程如何创建自定义集合视图单元格。

    【讨论】:

      【解决方案3】:

      斯威夫特 4.2

      我来到这里是因为同样的问题,但对于 Swift,我使用它修复了它,我希望它会有所帮助:

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
          let title = UILabel(frame: CGRect(x: 0, y: 0, width: cell.bounds.size.width, height: 50))
          title.text = "Some random text"
          title.font = UIFont(name: "AvenirNext-Bold", size: 15)
          title.textAlignment = .center
          cell.contentView.addSubview(title)
          return cell
      }
      

      不要忘记注册单元格:

      collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
      

      【讨论】:

      • 这种方法的问题是每次出列单元格时都会创建一个新标签。因为集合视图是一个回收视图,它最终会为您提供一个您以前使用过但当前未显示的单元格。这是为了提高效率,因为这意味着它不必一直创建一个新单元,这很昂贵。然后,此代码将在前一个标签之上添加另一个标签,让您一团糟。最好创建一个 UICollectionViewCell 的子类,其中只有一个文本视图。
      【解决方案4】:

      我有同样的问题,所以我做了一个迷你教程来做这个:How to make a simple collection view with Swift。代码在 Swift 中,但在 Objective C 中的过程大致相同。

      主要步骤是

      • 将 UICollectionView 添加到情节提要中的视图控制器
      • 将 UILabel 添加到集合视图单元格中。
      • 创建UICollectionViewCell 的自定义子类来保存单元格标签出口。
      • 让 Collection View Cell 使用该类。
      • 在 View Controller 中实现 UICollectionViewDataSourceUICollectionViewDelegate 及其方法。
      • 连接所有插座。
      • 使用数组或其他数据源中的字符串来填充单元格。

      【讨论】:

        【解决方案5】:

        像其他已经发布的一样,有UILabel 类型的属性。如果您不了解某个课程,请始终按 CMD 并选择您正在学习的课程。对于UICollectionView,您会看到,没有定义属性(Foundation 内的UICollectionViewCell 类:

        NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewCell : UICollectionReusableView
        
        @property (nonatomic, readonly) UIView *contentView; // add custom subviews to the cell's contentView
        
        // Cells become highlighted when the user touches them.
        // The selected state is toggled when the user lifts up from a highlighted cell.
        // Override these methods to provide custom UI for a selected or highlighted state.
        // The collection view may call the setters inside an animation block.
        @property (nonatomic, getter=isSelected) BOOL selected;
        @property (nonatomic, getter=isHighlighted) BOOL highlighted;
        
        // The background view is a subview behind all other views.
        // If selectedBackgroundView is different than backgroundView, it will be placed above the background view and animated in on selection.
        @property (nonatomic, retain) UIView *backgroundView;
        @property (nonatomic, retain) UIView *selectedBackgroundView;
        
        @end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-23
          相关资源
          最近更新 更多