【问题标题】:How to change label text place in collectionveiw header at runtime?如何在运行时更改集合视图标题中的标签文本位置?
【发布时间】:2021-03-26 09:58:38
【问题描述】:

this is screenshot(image) of my viewcontroller我正在使用collectionview并在标题中放置标签,在情节提要中创建标题和标签 我想在运行时更改标签文本。

我知道我可以在 collectionview 的 viewForSupplementaryElementOfKind 中做到这一点,但我希望在 viewdidload 方法中做到这一点

我的代码如下

控制器代码


class DeleteViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.backgroundColor = .red
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath) as! TestCollectionReusableView
        headerView.labelText.text = "dummy" // this line shows dummy
        return headerView
        
    }

   let testCollectionReusableView = TestCollectionReusableView()
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.register(TestCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerId")
        testCollectionReusableView.labelText.text = "Test" 
// above line Xcode 12.4 shows error - **Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value**
        
    }


}

头类文件


class TestCollectionReusableView: UICollectionReusableView {
        
    @IBOutlet weak var labelText: UILabel!
    
}

【问题讨论】:

    标签: ios swift uicollectionview uicollectionreusableview


    【解决方案1】:

    假设这个类包含您的集合视图并且是它的数据源,那么执行此操作的最佳位置可能是在您的 DeleteViewController 中。

    您可以简单地添加一个新属性,例如headerText: String,然后它可能是您数据源方法中的用户。每当您更改文本时,您都应该重新加载您的集合视图(或只是标题)以使更改生效。

    例如

    class DeleteViewController: UIViewController, UICollectionViewDataSource {
        
        @IBOutlet private var collectionView: UICollectionView?
    
        private var currentHeaderText: String = "Dummy"
    
        override func viewDidLoad() {
            super.viewDidLoad()
            changeHeaderText(to: "Some other text")
        }
    
        private func changeHeaderText(to text: String) {
            currentHeaderText = text
            collectionView?.reloadData()
        }
    
        func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath) as! TestCollectionReusableView
            headerView.labelText.text = currentHeaderText
            return headerView
        }
    
    }
    

    一种更优化的方法可能是检测哪些索引路径需要重新加载,并且只重新加载那些索引路径。甚至可以使用collectionView?.visibleSupplementaryViews(ofKind: <#T##String#>) 之类的方法并循环遍历与您的类对应的所有可见视图以应用更改。但这完全取决于您。

    【讨论】:

    • 我在 collectionview 的顶部只有一个 Header
    • @Dhiren 都一样
    • 嗨,它有效。 @Matic Oblak 有什么方法可以做同样的事情吗?
    • @Dhiren 你这是什么意思?你的意思是有一个方法可以更改文本而不调用重新加载到集合视图?
    • 不,我的意思是不采用“currentHeaderText”并触摸 viewForSupplementaryElementOfKind 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多