在swift中解除tableView后再次看复选标记

时间:2015-10-10 18:06:16

标签: ios swift uitableview checkmark

我有一个包含3个部分(服务,可用性和天数)的表视图,每个部分都有多行。我选择任何一行时都实现了一个复选标记。现在,我想在第一次关闭它后再次显示表时再次看到所选行。就像显示所选项目的历史一样。

这是显示复选标记的代码:

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

现在这里我从所选行获取数据的方法:

CREATE TABLE tmp (
    INDEX(someColumnProperty)
)
SELECT ...;

2 个答案:

答案 0 :(得分:0)

表视图解除后,选择状态本身会丢失。没有照顾它,它只是国家的一部分,观点管理。

save方法中,您正在存储“选择状态”。您有以下selectedServicesselectedAvailabilitiesselectedDays个变量存储选择状态。如果在第二次显示表格视图时仍然有这些,则可以从中提取选择状态。

可能你更改了updateCell调用:

updateCell(indPath, select: false)

到此:

updateCell(indPath, select: selectionState(indPath))

并定义一个这样的方法:(注意selectedServices.contains("RECEIVE")

func selectionState(indexpath: NSIndexPath) -> Bool {
    let indPath = indexPath
    let section = indPath.section
    switch section {
    case 0:
        switch indexPath.row{
            case ServicesIndex.Recieve.rawValue:
                return selectedServices.contains("RECEIVE")
            ...
    case 1:
        updateCell(indPath, select: false)
    case 2:
        updateCell(indPath, select: false)
    default:
        Void()
    }
}

contains方法实际上从数据模型中提取出选择状态(这些实例是selectedServices引用的这些实例等)。如果无法实现contains方法,则可能必须创建仅存储选择状态的数据结构。然后,您从该数据结构中读取选择状态,并将选择状态写入数据结构和selectedServices等实例。

答案 1 :(得分:0)

我添加了一个委托方法,在我的过滤器中添加了以下内容:

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
            }