gpt4 book ai didi

ios - Swift:当 UITableView 滚动太快时应用程序崩溃

转载 作者:可可西里 更新时间:2023-10-31 23:44:29 26 4
gpt4 key购买 nike

我正在创建一个页面,用户可以在其中从 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 的新手,所以如果我的代码中有任何错误,请告诉我!提前致谢:)

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)
}

将选定的字符串传输到主配置文件创建 View Controller

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]
}
}

提高转回主配置文件创建 View Controller 的速度

override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {

if identifier == "selectionCompleted" {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.performSegueWithIdentifier("selectionCompleted", sender: nil)
})
}
}

最佳答案

基于您提供的上下文。我猜这个问题可能是因为你的“内容定义方法”和选择的检查机制。

我的猜测:

在你的 tableView(:UITableView:NSIndexPath) -> UITableViewCell 函数中,每次代表请求单元格时,它都会循环传递您的 selectedArray。

并且由于 NSArray(array) 不是线程安全的,对 selectedCell 数组的任何更改或快速访问都可能导致各种状态。当“滚动得如此之快”会产生许多同时访问同一个数组的循环时,我认为这里可能会发生一些冲突。

我给个建议。您可以使用 Set 而不是数组。使用“contain”“insert”“remove”来控制状态,每个单元只管理它自己。

可能是这样的:

let cell = ...
...
if selectedSet.contains(indexPath.row){
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}else{
cell.accessoryType = UITableViewCellAccessoryType.None
}
...
return cell

关于ios - Swift:当 UITableView 滚动太快时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33405565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com