繁体   English   中英

如何将 IBOutlet 添加到另一个文件中的子类?

[英]How do I add an IBOutlet to subclass in another file?

我浏览了 Apple 的书“在 Swift 数据集合中开发”,起点如下:

我们有一个带有“Label Outlet”的表格视图 controller

我们有一个名为“EmojiTableViewController”的第一个 Swift 文件,主文件为 class。这个 class 连接到 ViewController

class EmojiTableViewController: UITableViewController {
}

我们有第二个 Swift 文件,名为“EmojiTableViewCell”,带有子类。这个 class 连接到单元格

class EmojiTableViewCell: UITableViewCell {
    @IBOutlet var nameLabel: UILabel!
}

目标是将第二个文件 (EmojiTableViewCell) 中的 IBOutlet 与 Storyboad 连接起来。 问题是,无论我做什么,“助手”都会打开主 class。我应该如何更改助手以显示子类,或者如何通过代码连接插座?

First create an empty XIB file and call it as EmojiTableViewXIB, then add TableViewCell from Xcode library and later set EmojiTableViewCell class to EmojiTableViewXIB.xib file. 

Secondly add below code to TableViewController file:
 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "EmojiTableViewCell", for: indexPath) as! EmojiTableViewCell
        cell.nameLabel.text = "My custom Text"
        return cell
    }
 override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(UINib(nibName: "EmojiTableViewCell", bundle: nil), forCellReuseIdentifier: "EmojiTableViewCell")
    }
    
Thirdly add create a Tableview file and set this file to view of TableViewController 

Lastly run you can run the code.

//MARK: You can find running code from the below GitHub repo link.

https://github.com/ahmetbostanciklioglu/IBOutletConnectionsFromCustomTableView.git

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM