在UICollectionViewCell中设置文本

6

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

在TableView中,我会这样做:

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

但我确实无法弄清楚如何为UIcollectionView中的项目做到这一点。

一个很好的起点:http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12。此外,Google也非常有帮助。 - fguchelaar
6个回答

16

UICollectionViewCell 没有默认的单元格样式。您需要创建一个自定义的 UICollectionViewCell 并在其中添加一个 UILabel


4

UICollectionViewCell没有像UITableviewCell那样的默认textLabel,您需要根据需要创建自定义UICollectionViewCell

您可以查看这个教程来学习如何创建自定义集合视图单元格。


4

Swift 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")

1
这种方法的问题在于每次出列单元格时都会创建一个新标签。因为集合视图是一个回收视图,它最终会给你一个以前使用过但当前未显示的单元格。这是为了效率,因为它意味着它不必一直创建新单元格,这是昂贵的。然后,此代码将在上一个标签顶部添加另一个标签,从而使您混乱不堪。创建一个只有一个文本视图的UICollectionViewCell子类可能更好。 - leafcutter
@leafcutter 你是对的,我感谢你的纠正。 - Joule87

2
自从iOS 14以来,可以使用UICollectionViewListCell类,该类具有此文本设置选项。苹果文档
var content = cell.defaultContentConfiguration()

// Configure content.
content.text = "Hello"

cell.contentConfiguration = content

1
我有同样的问题,所以我制作了一个小教程来解决这个问题:如何使用Swift创建简单的集合视图。代码是用Swift编写的,但Objective C的过程大致相同。
主要步骤如下:
- 在Storyboard中向ViewController添加UICollectionView。 - 向Collection View Cell添加UILabel。 - 制作一个自定义的UICollectionViewCell子类来保存cell Label outlet。 - 让Collection View Cell使用该类。 - 在ViewController中实现UICollectionViewDataSourceUICollectionViewDelegate及其方法。 - 连接所有outlets。 - 使用数组或其他数据源中的字符串来填充单元格。

0

和其他人已经发表的一样,存在一个类型为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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接