3 回答

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用Swift 5和iOS 12.2,您應(yīng)該嘗試以下代碼來(lái)解決您的問(wèn)題:
CustomCell.swift
import UIKitclass CustomCell: UITableViewCell { // Link those IBOutlets with the UILabels in your .XIB file @IBOutlet weak var middleLabel: UILabel! @IBOutlet weak var leftLabel: UILabel! @IBOutlet weak var rightLabel: UILabel!}
TableViewController.swift
import UIKitclass TableViewController: UITableViewController { let items = ["Item 1", "Item2", "Item3", "Item4"] override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell") } // MARK: - UITableViewDataSource override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell cell.middleLabel.text = items[indexPath.row] cell.leftLabel.text = items[indexPath.row] cell.rightLabel.text = items[indexPath.row] return cell }}
下圖顯示了一組使用提供的代碼的約束,而沒(méi)有來(lái)自Xcode的任何約束歧義消息:

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
這是我使用Swift 2和Xcode 7.3的方法。此示例將使用單個(gè)ViewController加載兩個(gè).xib文件 - 一個(gè)用于UITableView,另一個(gè)用于UITableCellView。
對(duì)于此示例,您可以將UITableView權(quán)限放入空的TableNib .xib文件中。在里面,將文件的所有者設(shè)置為ViewController類,并使用插座引用tableView。
和
現(xiàn)在,在視圖控制器中,您可以像往常一樣委托tableView,就像這樣
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! ... override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // Table view delegate self.tableView.delegate = self self.tableView.dataSource = self ...
要再次創(chuàng)建自定義單元格,請(qǐng)將Table View Cell對(duì)象拖放到空的TableCellNib .xib文件中。這次,在單元格.xib文件中,您不必指定“所有者”,但您需要指定自定義類和標(biāo)識(shí)符,如“TableCellId”
使用您需要的任何插件創(chuàng)建您的子類
class TableCell: UITableViewCell { @IBOutlet weak var nameLabel: UILabel!}
最后......回到View Controller中,您可以加載并顯示整個(gè)事物
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // First load table nib let bundle = NSBundle(forClass: self.dynamicType) let tableNib = UINib(nibName: "TableNib", bundle: bundle) let tableNibView = tableNib.instantiateWithOwner(self, options: nil)[0] as! UIView // Then delegate the TableView self.tableView.delegate = self self.tableView.dataSource = self // Set resizable table bounds self.tableView.frame = self.view.bounds self.tableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] // Register table cell class from nib let cellNib = UINib(nibName: "TableCellNib", bundle: bundle) self.tableView.registerNib(cellNib, forCellReuseIdentifier: self.tableCellId) // Display table with custom cells self.view.addSubview(tableNibView)}
代碼顯示了如何簡(jiǎn)單地加載和顯示nib文件(表),以及如何注冊(cè)nib以供單元使用。
希望這可以幫助?。?!

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
斯威夫特4
注冊(cè)Nib
tblMissions.register(UINib(nibName: "MissionCell", bundle: nil), forCellReuseIdentifier: "MissionCell")
在TableView DataSource中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "MissionCell", for: indexPath) as? MissionCell else { return UITableViewCell() } return cell }
- 3 回答
- 0 關(guān)注
- 1107 瀏覽
添加回答
舉報(bào)