0

3 つのセクション (Services、Availability、Days) とそれぞれに複数の行を含むテーブル ビューがあります。行を選択するとチェックマークが表示されるように実装しました。ここで、最初にテーブルを閉じた後、テーブルを再度表示するときに、選択した行をもう一度表示したいと考えています。選択したアイテムの履歴を表示するように。

チェックマークを表示するコードは次のとおりです。

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