Ios 在swift中取消表格视图后再次查看复选标记
我有一个包含服务、可用性和天数三个部分的表视图,每个部分都有多行。我实现了一个复选框,以便在选择任何行时看到复选标记。现在,我想在第一次关闭表格后再次显示表格时再次查看选定的行。例如显示所选项目的历史记录 以下是显示复选标记的代码:Ios 在swift中取消表格视图后再次查看复选标记,ios,swift,uitableview,checkmark,Ios,Swift,Uitableview,Checkmark,我有一个包含服务、可用性和天数三个部分的表视图,每个部分都有多行。我实现了一个复选框,以便在选择任何行时看到复选标记。现在,我想在第一次关闭表格后再次显示表格时再次查看选定的行。例如显示所选项目的历史记录 以下是显示复选标记的代码: override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let indPath = indexPath let
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indPath = indexPath
let section = indPath.section
switch section {
case 0:
updateCell(indPath, select: true)
case 1:
updateCell(indPath, select: true)
case 2:
updateCell(indPath, select: true)
default:
Void()
}
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let indPath = indexPath
let section = indPath.section
switch section {
case 0:
updateCell(indPath, select: false)
case 1:
updateCell(indPath, select: false)
case 2:
updateCell(indPath, select: false)
default:
Void()
}
}
func updateCell(indexpath: NSIndexPath, select: Bool) {
if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: indexpath.row, inSection: indexpath.section)) {
if select{
cell.accessoryType = .Checkmark
}
else {
cell.accessoryType = .None
}
}//end if cell
现在,这里是我从所选行获取数据的方法:
func save(){
if let indexPaths = self.tableView.indexPathsForSelectedRows{
for indexPath in indexPaths{
switch indexPath.section{
case SectionIndex.Services.rawValue:
switch indexPath.row{
case ServicesIndex.Recieve.rawValue:
selectedServices.insert("RECEIVE")
case ServicesIndex.Send.rawValue:
selectedServices.insert("SEND")
default:
Void()
}//end switch
case SectionIndex.Availability.rawValue:
switch indexPath.row{
case AvailabiltiyIndex.Morning.rawValue:
selectedAvailabilities.append(6.0...9.0)
case AvailabiltiyIndex.Regular.rawValue :
selectedAvailabilities.append(9.0...18.0)
case AvailabiltiyIndex.Evening.rawValue:
selectedAvailabilities.append(18.0...21.0)
default:
Void()
}//end switch
case SectionIndex.Days.rawValue:
switch indexPath.row{
case DaysIndex.Sunday.rawValue:
selectedDays.insert("Sunday")
case DaysIndex.Monday.rawValue:
selectedDays.insert("Monday")
case DaysIndex.Tuesday.rawValue:
selectedDays.insert("Tuesday")
case DaysIndex.Wednesday.rawValue:
selectedDays.insert("Wednesday")
case DaysIndex.Thursday.rawValue:
selectedDays.insert("Thursday")
case DaysIndex.Friday.rawValue:
selectedDays.insert("Friday")
case DaysIndex.Saturday.rawValue:
selectedDays.insert("Saturday")
default:
Void()
}//end switch
default:
Void()
}//end switch
}//end for
}//end If
}//end save
取消表视图后,选择状态本身将丢失。如果不处理它,它只是视图管理的状态的一部分 在保存方法中,您正在存储选择状态。您可以使用这些selectedServices、SelectedAvailability和selectedDays变量来存储选择状态。如果在第二次显示表视图时仍然存在这些状态,则可以从中提取选择状态 您很可能会从以下位置更改updateCell调用:
updateCell(indPath, select: false)
为此:
updateCell(indPath, select: selectionState(indPath))
并定义如下方法:注意selectedServices.containsRECEIVE
contains方法实际上从数据模型中提取选择状态,这些数据模型是selectedServices等引用的实例。。如果无法实现contains方法,则可能必须创建一个仅存储选择状态的数据结构。然后从该数据结构读取选择状态,并将选择状态写入数据结构和selectedServices等实例。我添加了一个委托方法,在我的筛选器中添加了以下内容:
protocol FilterSettingsDelegate:class{
func filtercontrollerWillDissmiss(data: Set<NSIndexPath>)
}
class FilterTableViewController: UITableViewController {
weak var delegate: FilterSettingsDelegate?
var filterSettings : Set<NSIndexPath>? = []
...
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.filterSettings!.insert(indexPath)
let indPath = indexPath
let section = indPath.section
switch section {
case 0:
updateCell(indPath, select: true)
....
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if self.filterSettings != nil {
self.filterSettings!.remove(indexPath)
}
let indPath = indexPath
let section = indPath.section
switch section {
case 0:
....
class SearchViewController: UIViewController, UISearchBarDelegate, FilterSettingsDelegate {
var filterSettings : Set<NSIndexPath>?
....
//Delegate Method
func filtercontrollerWillDissmiss(data: Set<NSIndexPath>) {
self.filterSettings = data
}
//Then pass the data in the segue
case "FilterSegue" :
let navController = segue.destinationViewController as! UINavigationController
if let controller = navController.topViewController as? FilterTableViewController{
if self.filterSettings != nil{
controller.filterSettings = self.filterSettings
}
controller.delegate = self
}
在我的地图视图中,我添加了以下内容:
protocol FilterSettingsDelegate:class{
func filtercontrollerWillDissmiss(data: Set<NSIndexPath>)
}
class FilterTableViewController: UITableViewController {
weak var delegate: FilterSettingsDelegate?
var filterSettings : Set<NSIndexPath>? = []
...
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.filterSettings!.insert(indexPath)
let indPath = indexPath
let section = indPath.section
switch section {
case 0:
updateCell(indPath, select: true)
....
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if self.filterSettings != nil {
self.filterSettings!.remove(indexPath)
}
let indPath = indexPath
let section = indPath.section
switch section {
case 0:
....
class SearchViewController: UIViewController, UISearchBarDelegate, FilterSettingsDelegate {
var filterSettings : Set<NSIndexPath>?
....
//Delegate Method
func filtercontrollerWillDissmiss(data: Set<NSIndexPath>) {
self.filterSettings = data
}
//Then pass the data in the segue
case "FilterSegue" :
let navController = segue.destinationViewController as! UINavigationController
if let controller = navController.topViewController as? FilterTableViewController{
if self.filterSettings != nil{
controller.filterSettings = self.filterSettings
}
controller.delegate = self
}
谢谢你的帮助。实际上,在取消表视图后,不会存储任何选定的数据。基本上,当我按下过滤器按钮导航到上面描述的过滤器表视图时,我有一个地图视图和一个过滤器按钮。我需要的是,在用户选择他需要的过滤器选项之后,当他返回到过滤器表视图时,他应该能够看到他选择的内容。我不熟悉斯威夫特和IOS。我不知道如何保存用户选择的内容以及如何检索回来:您有一个视图控制器VC,用于显示过滤器表视图FVC。在VC中保留存储选择状态的数据结构。使用prepareForSegue google it将其传递给FVC。然后,您可以在FVC中更新数据结构,当您再次打开FVC时,数据结构将再次出现。数据结构可以是字典行的数组部分,其中的值为Bools true/false=selected/notselected。