亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從Swift中的nib定制UITableViewCell

從Swift中的nib定制UITableViewCell

慕姐8265434 2019-08-06 15:54:48
從Swift中的nib定制UITableViewCell我正在嘗試從筆尖創建自定義表格視圖單元格。我在這里指的是這篇文章。我面臨兩個問題。我創建了一個.xib文件,其中拖動了一個UITableViewCell對象。我創建了一個子類,UITableViewCell并將其設置為單元格的類,將Cell設置為可重用的標識符。import UIKitclass CustomOneCell: UITableViewCell {     @IBOutlet weak var middleLabel: UILabel!     @IBOutlet weak var leftLabel: UILabel!     @IBOutlet weak var rightLabel: UILabel!     required init(coder aDecoder: NSCoder!) {         super.init(coder: aDecoder)     }     override init(style: UITableViewCellStyle, reuseIdentifier: String!) {         super.init(style: style, reuseIdentifier: reuseIdentifier)     }     override func awakeFromNib() {         super.awakeFromNib()         // Initialization code    }     override func setSelected(selected: Bool, animated: Bool) {         super.setSelected(selected, animated: animated)         // Configure the view for the selected state    }}在故事板中的UITableViewController中,我沒有對單元格做任何事情。空標識符,沒有子類。我嘗試將Cell標識符添加到原型單元格并再次運行但我得到了相同的結果。如我在文章中所提到的,我將cell參數的類型形式UITableViewCell更改CustomOneCell為我的UITableViewCell的子類。但我得到以下錯誤,使用選擇器'tableView重寫方法:willDisplayCell:forRowAtIndexPath:'具有不兼容的類型'(UITableView!,CustomOneCell!,NSIndexPath!) - >()'任何人都知道如何解決這些錯誤?這些似乎在Objective-C中運行良好。謝謝。
查看完整描述

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的任何約束歧義消息:


查看完整回答
反對 回復 2019-08-06
?
犯罪嫌疑人X

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以供單元使用。

希望這可以幫助?。?!


查看完整回答
反對 回復 2019-08-06
?
慕斯王

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
    }


查看完整回答
反對 回復 2019-08-06
  • 3 回答
  • 0 關注
  • 1108 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號