IOS Swift CollectionView 如何在索引上设置正确的背景颜色 [英] IOS Swift CollectionView How can I set the correct background color on index

查看:33
本文介绍了IOS Swift CollectionView 如何在索引上设置正确的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 CollectionView .我有一个简单的按钮,因为我用字符串数组填充它,所以它重复了 9 次.我想更改第一个元素的背景颜色,我可以这样做,但是在 Scroll 我也得到具有相同背景颜色的其他元素,我只希望第一个元素具有该背景颜色.这是我的代码

This is my first time working with a CollectionView . I have a simple button which is repeated 9 times since I am populating it with a string array . I want to change the background color of the first element and I can do that however on Scroll I get other elements with that same background color too and I only want the first element to have that background color . This is my code

class HomeC: UIViewController,CollectionViewDelegate,UICollectionViewDataSource {
    @IBOutlet weak var CategoriesView: UICollectionView!
    var CategoryIndex = 0

    var categoryTypes = ["All","General","Sports","Local Issues","Questions","Politics","Events","Advertise","Sell","Services"]

    override func viewDidLoad() {
        super.viewDidLoad()
        CategoriesView.delegate = self
        CategoriesView.dataSource = self
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return categoryTypes.count
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoriesCVC", for: indexPath) as! CategoriesCVC
        cell.items.tag = indexPath.row
        cell.items.setTitle(categoryTypes[indexPath.row], for: .normal)

        if categoryTypes[indexPath.row] == "All" && indexPath.row == CategoryIndex {

            cell.items.backgroundColor = UIColor(hexString: BlueBackground)
            cell.items.setTitleColor(.white, for: .normal)
        } 
        return cell
    }
}

使用该代码,我的第一个元素全部"具有适当的背景,但是如果我滑过 collectionView,我会得到其他带有蓝色背景的元素.我该如何解决这个问题?任何建议都会很棒.

With that code I have my first element which is "All" has the proper background however if I slide across the collectionView I get other elements also with the blue background . How can I fix this issue ? any suggestions would be great .

推荐答案

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoriesCVC", for: indexPath) as! CategoriesCVC
        cell.items.tag = indexPath.row
        cell.items.setTitle(categoryTypes[indexPath.row], for: .normal)

        if categoryTypes[indexPath.row] == "All" && indexPath.row == CategoryIndex {

            cell.items.backgroundColor = UIColor(hexString: BlueBackground)
            cell.items.setTitleColor(.white, for: .normal)
        } else {
            // set your normal cell color here
            cell.items.backgroundColor = UIColor(hexString: NormalCellColor)
            cell.items.setTitleColor(.normalTitleColoe, for: .normal)
     }
        return cell
}

由于集合视图使用出列单元格,所以它采用旧单元格背景.那是蓝色.如果您恢复 else 块中的单元格颜色,这将解决您的问题

Since collection view uses the dequeued cell, it takes the old cell background. that is blue color. if you revert the cell color in the else block, that will fix your issues

如果你想以更专业的方式做.在单元格 prepareForReuse 上,进行清理.

if you want to do in more Professional way. on cell prepareForReuse, do your cleanups.

override open func prepareForReuse() {
    super.prepareForReuse()
    // Set your default background color, title color etc
}

这篇关于IOS Swift CollectionView 如何在索引上设置正确的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆