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

标签 ios swift uitableview uicollectionview swift4

我有一个在主 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 view of the XIB

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

希望对你有帮助!

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

相关文章:

ios - TWICE相关实体的CoreData计数记录

swift - 如何在 Swift 中使用 println 格式化数字

ios - 每次向下滚动时如何在 UITableView 上居中显示加载 View ?

iphone - 重复使用表格单元格问题

ios - 设置 UITableViewCell backgroundView 和 selectedBackgroundView 在 iOS7 中不起作用

iphone - UITabBarController 不显示 tabbarItems 名称

ios - ios swift 中垂直 ScrollView 内的水平 ScrollView 问题

ios - 根据动态本地内容确定 UIWebView 高度,在可变高度 TableViewCell 内

swift - 在 swift 中映射高阶函数格式

json - 解析字典行为奇怪