【发布时间】: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
【问题讨论】:
-
你是使用原型单元还是单独的xib?
-
感谢您的所有评论!修复它并忘记回复;-;
标签: swift