gpt4 book ai didi

ios - Swift 4 : Xib with collection view that reuse cell from storyboard, 内部导出为零

转载 作者:行者123 更新时间:2023-11-30 11:18:57 25 4
gpt4 key购买 nike

我有一个在主 Storyboard Collection View 中使用的单元格:

class TaskCell: UICollectionViewCell {

@IBOutlet weak var exerciseTitle: UILabel!


override func awakeFromNib() {
super.awakeFromNib()
self.layer.borderWidth = 1
self.layer.borderColor = #colorLiteral(red: 0.6666666865, green: 0.6666666865, blue: 0.6666666865, alpha: 1)
}

}

我创建了一个带有 Collection View 的 XIB,并且我尝试在 XIB 的 Collection View 中使用此单元格。该单元已出列,但其中的所有导出均为零

class AddTaskToSessionVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var collectionView: UICollectionView!
public var allTasks = [TaskCodableEntity]()

override func awakeFromNib() {
super.awakeFromNib()
}

override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.register(TaskCell.self, forCellWithReuseIdentifier: "TaskCell")
self.getCurrentPatientTasks()

// Do any additional setup after loading the view.
}

public func getCurrentPatientTasks() {

self.allTasks = API.getTasks()
}
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.collectionView.showNoDataLabel(array: self.allTasks)
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "TaskCell", for: indexPath) as? TaskCell {
let taskEntity = self.allTasks[indexPath.row]
cell.exerciseTitle.text = taskEntity.title
***(Here exerciseTitle is nil)***

return cell
}
return UICollectionViewCell()
}
}

我的 XIB 看起来像:

enter image description here

我尝试在单元格内实例化标签,但它仍然保持为零有没有办法以这种方式重用单元,或者我应该创建 XIB 单元,尽管已经存在单元?

最佳答案

为了您可以轻松地使用 xib 来使用 View 并重用它,我建议您查看框架 Reusable

我认为在这句话中你可以找到你问题的答案。我认为您需要为您的类创建文件所有者(而不是 View ),并且您可以使用 NibOwnerLoadableReusable 协议(protocol)来更轻松地重用和加载您的 View 。

Declare your views to conform to NibLoadable or NibOwnerLoadable

Use the NibLoadable protocol if the XIB you're using don't use its"File's Owner" and the reusable view you're designing is the root viewof the XIB

Use the NibOwnerLoadable protocol if you used a "File'sOwner" of the XIB being of the class of your reusable view, and theroot view(s) of the XIB is to be set as a subview providing itscontent.

希望对你有帮助!

关于ios - Swift 4 : Xib with collection view that reuse cell from storyboard, 内部导出为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51493880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com