簡體   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