如何在选定的UicollectionView单元格上显示计数标签?

发布于 2025-02-07 21:12:03 字数 195 浏览 1 评论 0原文

我有一个CalendArcollectionView,它显示为每月日历类似于iOS日历。因此,在该日历上,我必须显示用户以前已经选择的预选日期。但是我还需要将标签显示为对这些单元的限制(假设如果用户的预选为23,24,25,26,则在这些datecell用户上也应将1,2,3,4分别为右角标签。 )。 我该怎么做? “ alt =”请参考附带的图像以获取更多理解”>

I have a CalendarCollectionView, which shows up as monthly calendar similar like iOS Calendar. So on that Calendar i have to show the PreselectedPeriod dates which user has already selected before. But i also need to show a label as a count on these cells (let's assume if user has Preselected values as 23,24,25,26 so on these dateCell user should also see 1,2,3,4 as right corner label respectively).
How can i do that?Please refer to image attached for more understanding

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

权谋诡计 2025-02-14 21:12:03

如果您已经选择了日期,则可以根据需要使用以下逻辑而在几乎没有修改的情况下使用以下逻辑。

您可以声明助手变量

// This is your list of pre-selected dates
let preselectedPeriod = [3,12,13,14,16,17,18,29,30]

// Counter variable to increase your selected dates range
var count = 0

cellforintemat看起来像这样。
注意:在这里我使用indexpath作为日历日期

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! Cell
    cell.layer.borderColor = UIColor.black.cgColor
    cell.layer.borderWidth = 0.5
    cell.dateLabel.text = "\(indexPath.row+1)"
    cell.rangeIndicatorLabel.textColor = .white
    cell.card.layer.cornerRadius = 5
    cell.card.layer.masksToBounds = true
    
    if preselectedPeriod.contains(indexPath.row+1) {
        count += 1
        cell.card.backgroundColor = UIColor.orange
        cell.dateLabel.textColor = .white
        cell.rangeIndicatorLabel.text = "\(count)"
    }else{
        cell.dateLabel.textColor = .black
        cell.card.backgroundColor = UIColor.white
        cell.rangeIndicatorLabel.text = ""
    }
    return cell
}

“在此处输入图像说明”

If you have already selected dates you can use the following logic with little modifications according to your requirement.

You can declare the helper variables

// This is your list of pre-selected dates
let preselectedPeriod = [3,12,13,14,16,17,18,29,30]

// Counter variable to increase your selected dates range
var count = 0

Your cellForIntemAt looks like this.
Note: Here I have used the indexPath as the calendar date

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! Cell
    cell.layer.borderColor = UIColor.black.cgColor
    cell.layer.borderWidth = 0.5
    cell.dateLabel.text = "\(indexPath.row+1)"
    cell.rangeIndicatorLabel.textColor = .white
    cell.card.layer.cornerRadius = 5
    cell.card.layer.masksToBounds = true
    
    if preselectedPeriod.contains(indexPath.row+1) {
        count += 1
        cell.card.backgroundColor = UIColor.orange
        cell.dateLabel.textColor = .white
        cell.rangeIndicatorLabel.text = "\(count)"
    }else{
        cell.dateLabel.textColor = .black
        cell.card.backgroundColor = UIColor.white
        cell.rangeIndicatorLabel.text = ""
    }
    return cell
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文