0

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

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

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
4

2 回答 2

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
            }
于 2015-10-11T17:44:33.213 回答
0

关闭表格视图后,选择状态本身会丢失。如果不关心它,它只是视图管理的状态的一部分。

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

您可能会从此更改 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方法实际上从数据模型中提取了选择状态(这些实例被selectedServicesetc. 引用)。如果无法实现某个contains方法,则可能必须创建一个仅存储选择状态的数据结构。然后,您从该数据结构中读取选择状态并将选择状态写入数据结构和selectedServices等实例。

于 2015-10-10T19:57:38.463 回答