【问题标题】:Get IndexPath Value in UICollectionViewCell Class在 UICollectionViewCell 类中获取 IndexPath 值
【发布时间】:2016-10-24 07:47:33
【问题描述】:

我有一个 UiCollectionViewCell 类如下:

class DateCollectionViewCell: UICollectionViewCell {

    let dateLabel = UILabel()
    override func awakeFromNib() {
        super.awakeFromNib()        

        dateLabel.frame = CGRectMake(0, 5, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))
        dateLabel.textAlignment = .Left
        dateLabel.font = UIFont(name: "HelveticaNeue", size: 13)

        dateLabel.adjustsFontSizeToFitWidth = false
        self.contentView.addSubview(dateLabel)

    }
}

我只想更改第一行第一列单元格标签的 Y 位置,其余单元格标签保持不变。因此,我需要 IndexPath 值,以便检查当前单元格的部分和行。

我怎样才能得到这个? 有没有其他方法可以做我想要的???

【问题讨论】:

  • 取决于您需要在哪里检索 indexPath。如果你有单元格,你可以像这样得到它:let index_path=collectionView?.indexPath(for: cell)。
  • 您的集合视图数据源知道它正在设置哪个单元格,因此它应该在单元格上设置一个属性,告诉它如何呈现。知道它正在显示哪个 indexPath 不是单元格的工作。

标签: ios swift uicollectionview nsindexpath


【解决方案1】:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

   let cell : DateCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(CellIdentifier, forIndexPath: indexPath) as! DateCollectionViewCell 

   if indexPath.section == 0 && indexPath.row == 0  {
        // do your custom changes for the cell
    }

   return cell // your default cell
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-11
    • 2017-07-29
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2020-03-21
    相关资源
    最近更新 更多