3 回答
TA貢獻1808條經驗 獲得超4個贊
使用Swift 5和iOS 12.2,您應該嘗試以下代碼來解決您的問題:
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
}}下圖顯示了一組使用提供的代碼的約束,而沒有來自Xcode的任何約束歧義消息:
TA貢獻2080條經驗 獲得超4個贊
這是我使用Swift 2和Xcode 7.3的方法。此示例將使用單個ViewController加載兩個.xib文件 - 一個用于UITableView,另一個用于UITableCellView。
對于此示例,您可以將UITableView權限放入空的TableNib .xib文件中。在里面,將文件的所有者設置為ViewController類,并使用插座引用tableView。
和
現在,在視圖控制器中,您可以像往常一樣委托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
...要再次創建自定義單元格,請將Table View Cell對象拖放到空的TableCellNib .xib文件中。這次,在單元格.xib文件中,您不必指定“所有者”,但您需要指定自定義類和標識符,如“TableCellId”
使用您需要的任何插件創建您的子類
class TableCell: UITableViewCell {
@IBOutlet weak var nameLabel: UILabel!}最后......回到View Controller中,您可以加載并顯示整個事物
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)}代碼顯示了如何簡單地加載和顯示nib文件(表),以及如何注冊nib以供單元使用。
希望這可以幫助?。?!
TA貢獻1864條經驗 獲得超2個贊
斯威夫特4
注冊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 關注
- 1108 瀏覽
添加回答
舉報
