我正在创建一个页面,用于在UITableView中从行业列表中选择用户所在的行业。这是一个配置档案过程的一部分。当用户选择行业时,它会被选中标记(如果之前没有选择),同时会被取消标记(如果之前已经选择)。
我已经编写了能够成功构建的代码,一切正常。但是,当我滚动得太快时,应用程序会崩溃,并显示以下错误:
2015-10-29 10:16:40.576 Protégé Version 1.0[10867:473794] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'
接下来是“First throw call stack”和所有文件的列表。奇怪的是,这似乎只发生在我滚动得太快时。而且通常突然间就会发生。
以下是我的代码。我已经搜索了与我得到的错误消息有关的任何内容,但在这种情况下似乎没有什么有用的东西。我无法找出问题在哪里,真的很感激任何帮助。我是Swift的新手,如果我的代码中有任何错误,请告诉我!先感谢大家:)
定义每个单元格的内容
创建一个数组来存储所选单元格。
将选定的字符串传输到主配置文件创建视图控制器。
我已经编写了能够成功构建的代码,一切正常。但是,当我滚动得太快时,应用程序会崩溃,并显示以下错误:
2015-10-29 10:16:40.576 Protégé Version 1.0[10867:473794] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'
接下来是“First throw call stack”和所有文件的列表。奇怪的是,这似乎只发生在我滚动得太快时。而且通常突然间就会发生。
以下是我的代码。我已经搜索了与我得到的错误消息有关的任何内容,但在这种情况下似乎没有什么有用的东西。我无法找出问题在哪里,真的很感激任何帮助。我是Swift的新手,如果我的代码中有任何错误,请告诉我!先感谢大家:)
var array = ["Industry 1", "Industry 2", "Industry 3", "Industry 4", "Industry 5"]
var itemsSelected: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
}
确定有多少行
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
定义每个单元格的内容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = array[indexPath.row]
for var i = 0; i < selectedCells.count; i++ {
let indexPath = NSIndexPath(forRow: selectedCells[i], inSection: 0)
let checkedCell = tableView.cellForRowAtIndexPath(indexPath)
checkedCell?.accessoryType = UITableViewCellAccessoryType.Checkmark
}
return cell
}
创建一个数组来存储所选单元格。
var selectedCells: [Int] = []
选择行 - 勾选和取消勾选
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let cell = tableView.cellForRowAtIndexPath(indexPath)
// Checkmark & un-checkmark cell
if cell!.accessoryType == UITableViewCellAccessoryType.None {
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
} else {
cell!.accessoryType = UITableViewCellAccessoryType.None
}
// Add cell # to selectedCells array if it's a new selection. Remove if it's a deselection.
if selectedCells.contains(indexPath.row) {
selectedCells = selectedCells.filter {$0 != indexPath.row}
} else {
selectedCells.append(indexPath.row)
}
print(selectedCells)
}
将选定的字符串传输到主配置文件创建视图控制器。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "selectionCompleted" {
// Get the new view controller using segue.destinationViewController
let destViewController: CreateProfileViewController = segue.destinationViewController as! CreateProfileViewController
// Pass the selected object to the new view controller.
destViewController.displayedSelection = array[0]
}
}
提高返回主配置文件创建视图控制器的过渡速度
override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
if identifier == "selectionCompleted" {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.performSegueWithIdentifier("selectionCompleted", sender: nil)
})
}
}