【问题标题】:Swift 'unable to dequeue a cell with identifier intervalCellIdentifierSwift'无法使具有标识符intervalCellIdentifier的单元格出列
【发布时间】:2016-07-28 14:17:01
【问题描述】:

我最近开始使用 swift(swift newbie! :b) 进行编程 遇到错误,不知道如何解决:c

这是 viewcontroller.swift 的代码!

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var picker1: UIPickerView!
@IBOutlet weak var keySelect: UITableView!

var Array = ["2", "3", "4"]

@IBOutlet weak var picker1label: UILabel!

@IBOutlet weak var keyselectView: UILabel!
@IBOutlet weak var keylabel: UIButton!

let intervalCellIdentifier = "intervalCellIdentifier"

var intervalNames = ["Q", "W", "E", "R", "T", "Y"]

let limit = 5

var answer1 = 0

override func viewDidLoad() {
    super.viewDidLoad()

    picker1.delegate = self
    picker1.dataSource = self

    //edited
    keySelect.delegate = self
    keySelect.dataSource = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}




func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

    return Array[row]

}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return Array.count
}


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

@IBAction func submit1(sender: AnyObject) {

    if(answer1 == 0) {
        picker1label.text = "2"
    }

    else if(answer1 == 1) {
        picker1label.text = "3"
    }

    else {
        picker1label.text = "4"
    }

}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    answer1 = row
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return intervalNames.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(intervalCellIdentifier,forIndexPath: indexPath) as UITableViewCell

    cell.accessoryType = .None
    cell.textLabel?.text = intervalNames[indexPath.row]

    return cell
}



//MARK: - UITableViewDelegate


func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

    if let sr = tableView.indexPathsForSelectedRows {
        if sr.count == limit {
            let alertController = UIAlertController(title: "Oops", message:
                    "You are limited to \(limit) selections", preferredStyle: .Alert)
            alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: {action in
                }))
            self.presentViewController(alertController, animated: true, completion: nil)

            return nil
        }
    }

    return indexPath
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    print("selected  \(intervalNames[indexPath.row])")

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        if cell.selected {
            cell.accessoryType = .Checkmark
        }
    }

    if let sr = tableView.indexPathsForSelectedRows {
        print("didDeselectRowAtIndexPath selected rows:\(sr)")
    }
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    print("deselected  \(intervalNames[indexPath.row])")

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        cell.accessoryType = .None
    }

    if let sr = tableView.indexPathsForSelectedRows {
        print("didDeselectRowAtIndexPath selected rows:\(sr)")
    }
}

}

2016-07-28 23:08:29.868 customkeyboard[60865:1611607] * -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] 中的断言失败,/BuildRoot/Library/Caches/com.apple.xbs/Sources/ UIKit_Sim/UIKit-3512.29.5/UITableView.m:6547 2016-07-28 23:08:29.945 customkeyboard[60865:1611607] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 intervalCellIdentifier 的单元格出列 - 必须注册一个 nib 或一个类用于标识符或连接故事板中的原型单元'

这是错误解释(?)

帮帮我:3

【问题讨论】:

标签: swift


【解决方案1】:

1.带XIB文件

你必须先将单元格注册到表格,可能在viewDidLoad,如:

let nib = UINib(nibName: "CustomCell", bundle: NSBundle.mainBundle()) 
tableView.registerNib(nib, forCellReuseIdentifier: "CustomCellIdentifier")

这适用于我们使用带有.xib 文件的自定义单元格。

2。没有XIB文件

如果我们不为自定义单元创建单独的.xib 文件,这是一个解决方案:

我们需要为表格视图中的单元格创建动态原型:

然后我们需要为我们的自定义单元格提供类名和重用标识符,如下所示:

在这里,无需注册class或nib!


这是cellForRowAtIndexPath 的实现:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Configure the cell...
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCellId", forIndexPath: indexPath) as! CustomTableViewCell
    cell.setCellIndexLabel(indexPath.row)

    return cell
}

上述两种解决方案的实现方式相同。


3.仅类实现
没有XIB,没有Dynamic Prototype

这是没有任何 XIB 或动态原型的自定义单元格:

class WithoutNibCustomTableViewCell: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    //do custom initialisation here, if any
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

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
}

class func identifier() -> String {
    return "WithoutNibCustomTableViewCellId"
}

//MARK: Public Methods
func setCellIndexLabel(index: Int) {
    let customLbl = UILabel(frame: CGRectMake(0, 0, 100, 40))
    customLbl.text = String(index)
    contentView.addSubview(customLbl)
}

}

然后,在表格视图中,我们需要将单元格类注册为:

tableView!.registerClass(WithoutNibCustomTableViewCell.classForCoder(), forCellReuseIdentifier: WithoutNibCustomTableViewCell.identifier())

cellForRowAtIndexPath也和上面一样,比如:

let cell = tableView.dequeueReusableCellWithIdentifier(WithoutNibCustomTableViewCell.identifier(), forIndexPath: indexPath) as! WithoutNibCustomTableViewCell
    cell.setCellIndexLabel(indexPath.row)
    return cell

【讨论】:

  • 请在代码中定义单元格而不是在 .xib 文件中时提供大小写来涵盖所有情况
  • 为这两种情况提供了解决方案,自定义单元格有和没有.xib 文件!
  • 没有 .xib - 只是在代码中,我的意思是,使用 registerClass,但你所做的工作令人印象深刻 =)
  • @ShadowOf: 3. 只有类实现是你的意思只是在代码中?
【解决方案2】:

在 cellForRow 中更改: let cell = tableView.dequeueReusableCellWithIdentifier(intervalCellIdentifier,forIndexPath: indexPath) as UITableViewCell by 让 cell = tableView.dequeueReusableCellWithIdentifier(intervalCellIdentifier,forIndexPath: indexPath) 作为你的细胞类名称。

【讨论】:

    【解决方案3】:

    我发布了一个 pod,可以让您在使用细胞时更轻松:

    https://cocoapods.org/pods/CellRegistration

    它将 API 简化为一行:

    let : MyCustomCell = tableView.dequeueReusableCell(forIndexPath: indexPath)
    

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 1970-01-01
      • 2014-03-02
      • 2015-01-23
      • 2013-10-05
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多