【问题标题】:Change cell border color in UICollectionView? [duplicate]更改 UICollectionView 中的单元格边框颜色? [复制]
【发布时间】:2016-02-27 15:10:09
【问题描述】:

我想在用户触摸单元格时更改边框颜色。我的 UICollectionView 中有以下代码,但它不起作用:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        self.user["avatar"] = self.avatars[indexPath.row]

        do {
           try self.user.save()
        } catch {
            print(error)
        }

        //self.performSegueWithIdentifier("returnToProfile", sender: user)
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! AvatarViewCell
        cell.layer.borderWidth = 5.0
        cell.layer.borderColor = UIColor.yellowColor().CGColor

    }

谢谢!

编辑:我在被标记为重复的问题中尝试了投票最多的答案,但它仍然不起作用。 cell.layer.borderColor = UIColor.yellowColor().CGColor 不会改变颜色。

也许这是我如何定义单元格的问题?

【问题讨论】:

  • 我尝试了那个问题的答案,但仍然没有用。我认为这可能是我如何定义单元格的问题
  • 您是否尝试更改内容视图的边框? cell.contentView.layer.borderWidth = 1.0 cell.contentView.layer.borderColor = UIColor.yellowColor().CGColor

标签: ios swift uicollectionview swift2


【解决方案1】:

我使用数据源在触摸时更改单元格背景:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! DateCell

        if !days.contains(Functions.tools.nextThreeWeeks().long[indexPath.row]) {
            days.append(cell.fullDate)
            cell.backgroundColor = UIColor(red:0.96, green:0.72, blue:0.35, alpha:1)
        } else {
            days = days.arrayRemovingObject(cell.fullDate)
            cell.backgroundColor = UIColor.whiteColor()
        }

        print(days)

    }

【讨论】:

  • 成功了!我需要更改我的单元格定义以使用cellForItemAtIndexPath
猜你喜欢
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 1970-01-01
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多