第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從Swift中的nib定制UITableViewCell

從Swift中的nib定制UITableViewCell

慕姐8265434 2019-08-06 15:54:48
從Swift中的nib定制UITableViewCell我正在嘗試從筆尖創(chuàng)建自定義表格視圖單元格。我在這里指的是這篇文章。我面臨兩個(gè)問(wèn)題。我創(chuàng)建了一個(gè).xib文件,其中拖動(dòng)了一個(gè)UITableViewCell對(duì)象。我創(chuàng)建了一個(gè)子類,UITableViewCell并將其設(shè)置為單元格的類,將Cell設(shè)置為可重用的標(biāo)識(shí)符。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中,我沒(méi)有對(duì)單元格做任何事情??諛?biāo)識(shí)符,沒(méi)有子類。我嘗試將Cell標(biāo)識(shí)符添加到原型單元格并再次運(yùn)行但我得到了相同的結(jié)果。如我在文章中所提到的,我將cell參數(shù)的類型形式UITableViewCell更改CustomOneCell為我的UITableViewCell的子類。但我得到以下錯(cuò)誤,使用選擇器'tableView重寫(xiě)方法:willDisplayCell:forRowAtIndexPath:'具有不兼容的類型'(UITableView!,CustomOneCell!,NSIndexPath?。?- >()'任何人都知道如何解決這些錯(cuò)誤?這些似乎在Objective-C中運(yùn)行良好。謝謝。
查看完整描述

3 回答

?
炎炎設(shè)計(jì)

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


查看完整回答
反對(duì) 回復(fù) 2019-08-06
?
犯罪嫌疑人X

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

希望這可以幫助?。?!


查看完整回答
反對(duì) 回復(fù) 2019-08-06
?
慕斯王

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
    }


查看完整回答
反對(duì) 回復(fù) 2019-08-06
  • 3 回答
  • 0 關(guān)注
  • 1107 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)