我最近开始使用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 / Cache / 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
答案 0 :(得分:2)
<强> 1。使用XIB文件
您必须先将单元格注册到表格,可以在viewDidLoad
中,如下:
let nib = UINib(nibName: "CustomCell", bundle: NSBundle.mainBundle())
tableView.registerNib(nib, forCellReuseIdentifier: "CustomCellIdentifier")
如果我们使用带有.xib
文件的自定义单元格,则此选项适用。
<强> 2。没有XIB文件
如果我们不为自定义单元格创建单独的.xib
文件,那么这是一个解决方案:
以下是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
答案 1 :(得分:0)
改变这个: 让cell = tableView.dequeueReusableCellWithIdentifier(intervalCellIdentifier,forIndexPath:indexPath)为 UITableViewCell by 将cell = tableView.dequeueReusableCellWithIdentifier(intervalCellIdentifier,forIndexPath:indexPath)设为您的单元类名称。
答案 2 :(得分:0)
我发布了一个pod,它可以让你在使用单元格时更轻松:
https://cocoapods.org/pods/CellRegistration
它将API简化为一行:
let : MyCustomCell = tableView.dequeueReusableCell(forIndexPath: indexPath)