在 UISearchController 和 FetchRequest 之间同步复选标记
Sync Checkmarks Between UISearchController and FetchRequest
我正在尝试在搜索视图和非搜索视图之间同步 accessoryType.Checkmark
。我已经尝试在 cellForRowAtIndexPath
上的几个不同位置设置 cell.accessoryType = .None
,但是当我在 fetchedResults
和搜索结果之间切换时,我总是得到随机复选标记。我不知道我搞砸了什么,但我相信这会是非常愚蠢的事情。
我已经设置了 UITableViewController
。加载时,我将其配置为显示 NSFetchRequest
中的项目。它完美运行。
我也设置了一个 UISearchController
。它完美运行并显示我想要的结果。
我遇到在搜索和fetchRequest 之间切换时出现随机选中标记的问题。我的一系列要保存的东西运行良好,正确的项目都在里面——勾选标记被弄脏了。任何反馈将不胜感激。我没主意了。
这是我在 UITableViewController
viewDidLoad
之前声明的相关属性 class:
// Create array to dump fetchResults
var unfilteredJingles = [Jingle]()
// Create array to store filtered search results
var filteredJingles = [Jingle]()
// Array where ones I want to save get added/removed
var jinglesToSave = [Jingle]()
// resultsSearchController
var resultsSearchController = UISearchController()
选中单元格后,我 didSelectRowAtIndexPath
配置了两件事:
1) 将该单元格的 jinglesToSave
附加到一个数组,并在该单元格旁边放置一个复选标记。这行得通。
2) 从 jinglesToSave
数组中删除该单元格的 jinglesToSave
。这也行。
我遇到的问题是当我在 searchResultsController.active
和 searchResultsController
之间切换时随机单元格被检查不活动。正确的单元格保持选中状态,但有时会选中随机单元格。
这是我的 cellForRowAtIndexPath
:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("jingleCell", forIndexPath: indexPath)
// instantiate a local array and assign it to filtered/unfiltered results based on whether
// the resultsSearchController is active
var jingleArray = [Jingle]()
if resultsSearchController.active {
// set local array to the filtered array
jingleArray = filteredJingles
let jingle = jingleArray[indexPath.row]
// Set labels for cell
cell.textLabel?.text = jingle.jingleDescription
cell.detailTextLabel?.text = jingle.jingleStar
} else {
// set the local array to the UN-filtered array
jingleArray = unfilteredJingles
// Get the jingle for the index
let jingle = jingleArray[indexPath.row]
// Set labels for cell
cell.textLabel?.text = jingle.jingleDescription
cell.detailTextLabel?.text = jingle.jingleStar
}
// Set checkmarks
if self.jinglesToSave.count > 0 {
// enumerate jinglesToSave...
for (indexOfJinglesToSave, jingleToSave) in jinglesToSave.enumerate() {
// if the object in the array of stuff to save matches the object in the index of the tableview
if jingleArray[indexPath.row].hashValue == jinglesToSave[indexOfJinglesToSave].hashValue {
// then set its accessoryView to checkmark
cell.accessoryType = .Checkmark
}
}
}
return cell
}
在您使单元格出队后,将单元格复选标记状态重置为默认状态(我猜没有复选标记)。它可能是一个细胞回收问题,复选标记出现在它不应该出现的细胞上。
我正在尝试在搜索视图和非搜索视图之间同步 accessoryType.Checkmark
。我已经尝试在 cellForRowAtIndexPath
上的几个不同位置设置 cell.accessoryType = .None
,但是当我在 fetchedResults
和搜索结果之间切换时,我总是得到随机复选标记。我不知道我搞砸了什么,但我相信这会是非常愚蠢的事情。
我已经设置了 UITableViewController
。加载时,我将其配置为显示 NSFetchRequest
中的项目。它完美运行。
我也设置了一个 UISearchController
。它完美运行并显示我想要的结果。
我遇到在搜索和fetchRequest 之间切换时出现随机选中标记的问题。我的一系列要保存的东西运行良好,正确的项目都在里面——勾选标记被弄脏了。任何反馈将不胜感激。我没主意了。
这是我在 UITableViewController
viewDidLoad
之前声明的相关属性 class:
// Create array to dump fetchResults
var unfilteredJingles = [Jingle]()
// Create array to store filtered search results
var filteredJingles = [Jingle]()
// Array where ones I want to save get added/removed
var jinglesToSave = [Jingle]()
// resultsSearchController
var resultsSearchController = UISearchController()
选中单元格后,我 didSelectRowAtIndexPath
配置了两件事:
1) 将该单元格的 jinglesToSave
附加到一个数组,并在该单元格旁边放置一个复选标记。这行得通。
2) 从 jinglesToSave
数组中删除该单元格的 jinglesToSave
。这也行。
我遇到的问题是当我在 searchResultsController.active
和 searchResultsController
之间切换时随机单元格被检查不活动。正确的单元格保持选中状态,但有时会选中随机单元格。
这是我的 cellForRowAtIndexPath
:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("jingleCell", forIndexPath: indexPath)
// instantiate a local array and assign it to filtered/unfiltered results based on whether
// the resultsSearchController is active
var jingleArray = [Jingle]()
if resultsSearchController.active {
// set local array to the filtered array
jingleArray = filteredJingles
let jingle = jingleArray[indexPath.row]
// Set labels for cell
cell.textLabel?.text = jingle.jingleDescription
cell.detailTextLabel?.text = jingle.jingleStar
} else {
// set the local array to the UN-filtered array
jingleArray = unfilteredJingles
// Get the jingle for the index
let jingle = jingleArray[indexPath.row]
// Set labels for cell
cell.textLabel?.text = jingle.jingleDescription
cell.detailTextLabel?.text = jingle.jingleStar
}
// Set checkmarks
if self.jinglesToSave.count > 0 {
// enumerate jinglesToSave...
for (indexOfJinglesToSave, jingleToSave) in jinglesToSave.enumerate() {
// if the object in the array of stuff to save matches the object in the index of the tableview
if jingleArray[indexPath.row].hashValue == jinglesToSave[indexOfJinglesToSave].hashValue {
// then set its accessoryView to checkmark
cell.accessoryType = .Checkmark
}
}
}
return cell
}
在您使单元格出队后,将单元格复选标记状态重置为默认状态(我猜没有复选标记)。它可能是一个细胞回收问题,复选标记出现在它不应该出现的细胞上。