2

我有一个类文件,它是该类的子UICollectionViewController类。在我的cellForItemAtIndexPath方法中,我试图设置textLabel单元格的。但是,完成中没有textLabel选项。这是迄今为止的方法:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
    // Configure the cell
    cell.backgroundColor = cellColor ? UIColor.blueColor() : UIColor.orangeColor()
    //cell.textLabel.text = "Text"???
    switch indexPath.item {
    case 0...5:
        cellColor = true
    case 6...13:
        cellColor = false
    default:
        cellColor = true
    }
}

我该怎么做才能添加textLabel到单元格中?

4

3 回答 3

6

与 tableViewCell 不同,UICollectionViewCell 没有 textLabel。您需要将 UILabel 添加到单元格的内容视图示例:

var title = UILabel(frame: CGRectMake(0, 0, cell.bounds.size.width, 40))
cell.contentView.addSubview(title)
于 2014-12-17T01:58:00.163 回答
1

斯威夫特 4

这是向集合视图单元格添加文本的 Swift 4 方法:

    let title = UILabel(frame: CGRect(x: 0, y: 0, width: cell.bounds.size.width, height: 40))
    title.textColor = UIColor.black
    title.text = "T"
    title.textAlignment = .center
    cell.contentView.addSubview(title)
于 2018-02-13T14:07:12.260 回答
0

使用文档,卢克。它在 Xcode 的“帮助”菜单下。

UICollectionViews 没有 textLabels - 这就是你没有完成的原因。

他们有内容视图。不过,您可以将文本放在内容视图中。任何 UIView 子类都可以。如果想要的只是文本(没有图形),那么你可以使用 UITextView 作为你的内容视图,然后分配给它的“文本”属性:

cell.contentView.text = "文本"

但是,您需要一个 UICollectionView 的子类,它已经为 contentViews 提供了 UITextViews。

于 2014-12-17T02:03:23.410 回答