2016-05-16 14 views
1

私はすぐに私のアラームアプリのための下の画像のようなリピートリストを作りたい、私は最善を尽くしたが、すべての試みは失敗した、どのように私はそれを行うことができます、データベース内の1つの属性の複数の値、配列が必要ですか、どのようにチェックリストを行うことができますか?私のプロジェクトはほぼ完了して、残された唯一の事は、私がアラームを繰り返す日リストを作成する

Days.swift Reminder.swift

verride func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 


     if segue.identifier == "presentDay" { 

      if let daysPickerVie 

wController = segue.destinationViewController as? Days { 
      let path = tableView.indexPathForSelectedRow 
      detailLabel.text = daysPickerViewController.days[(path?.row)!] 
     } 
    } 
} 

事前

感謝を作るために失敗を繰り返しリストです

class Days: UITableViewController 
{ 
    var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0) 

    let days = [ 
     "Everyday", 
     "Saturday", 
     "Sunday", 
     "Monday", 
     "Tuesday", 
     "Wednesday", 
     "Thursday", 
     "Friday"] 


    var selectedDay:String? 
    var selectedDayIndex:Int? 
    var cell:UITableViewCell? 


    override func viewDidLoad() { 
     //navigationController?.navigationBar.topItem?.title = "Back" 
    } 

extension Days{ 


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return days.count 
     } 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      let cell = tableView.dequeueReusableCellWithIdentifier("daySelected", forIndexPath: indexPath) as UITableViewCell 


    cell.accessoryType = .None 
    cell.textLabel?.text = days[indexPath.row] 
    selectedDay = days[indexPath.row] 
    return cell 
    } 
} 

extension Days { 


     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


      if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
       if cell.selected { 
        cell.accessoryType = .Checkmark 
       } 
       } 

     } 
    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 


      if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
       cell.accessoryType = .None 
      } 


     } 

    } 

enter image description here

+0

あなたがしようとしたどのようなコードをコメントしてくださいすることができます –

+0

これは役立つかもしれない:http://stackoverflow.com/questions/21481987/repeat-uilocalnotification-on-specific-day –

+0

@ Ramkumar chintalaコードを追加しました – User

答えて

1

ここで最も適切な方法は、良い古いビットマスクだと思います。ビット単位の演算は多くの人々にとっては難しいことを認識しています。[1]ビット単位の演算子なしで動作する解決策を以下で見つけてください。属性は、タイプIntにすることができ、それは以下のロジックで全ての可能な変形をキャプチャすることができる:

0  no days selected 
1  Sundays selected 
2  Mondays selected 
4  Tuesdays selected 
8  Wednesdays selected 
// etc... 

格納される値は、そう例えば、すべての選択の合計であります毎週月曜日と金曜日は2 + 32 = 34となります。

エレガントなstructを入力して、生の値から選択した日のリストに変換することができます。

[1] 世界には10種類の人がいます。バイナリ数を理解している人とそうでない人。

+0

もう2つの追加の質問は、このプロセスのための切り替えが必要ですか?例えば、月曜日と火曜日には 'case:6'、日曜日や火曜日には' case5'です。すべての可能性; 2番目の質問は、選択した日をアラームビューに渡そうとすると失敗します。私は 'prepareForSegue'関数を使用しますが、リピートリストから返される値は' everyday'です。これは解決することができます – User

+0

これは本当に異なる問題です。あなたは新しい質問を開いて質問してください。 - 短い答え:いいえ、この場合スイッチは適切ではありません。あなたがこの設定の算術を理解していない場合、おそらくあなたはこれであなたの頭の上にあります。 - これは、データ構造に関する質問は、あなたが持つかもしれないUIKitの問題から独立しています。 – Mundi

+0

ありがとうムンディ、それは節約のための素晴らしいアプローチです。ありがとうございます。私は将来の参照のためにデータ質問の解析に関する回答を投稿しています – toiavalle

0

私は同じ問題を抱えていましたが解決しました。 "didDeselectRowAtIndexPath"メソッドは必要ありません。あなたのコードは以下のように更新します。ムンディに関するユーザーの質問に対して

var selectedIndexPaths = [IndexPath]() 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
    let day = days[(indexPath as NSIndexPath).row] 
    cell.textLabel?.text = day 
    if self.selectedIndexPaths.contains(indexPath) { 
     cell.accessoryType = .checkmark 
    } 
    else { 
     cell.accessoryType = .none 
    } 

      return cell 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath) 
    if self.selectedIndexPaths.contains(indexPath) { 
     self.selectedIndexPaths.remove(at: self.selectedIndexPaths.index(of: indexPath)!) 
     cell?.accessoryType = .none 
    } 
    else { 
     cell?.accessoryType = .checkmark 
     self.selectedIndexPaths.append(indexPath) 
    } 
} 
0

回答は、今後の参考のために 回答:あなたはスイッチを必要としません。データをバイナリとして解析するだけです。つまり、 バイナリの3は11 - >最初の1は2の場合で、2番目の場合は1の場合です。したがって、2進数の最後の数字は1で、次に日曜日が選択されます。右から2番目が1の場合、火曜日が選択されます。

14 
In binary: 1110 
1st case from right is 0 -> Sunday is not selected 
2nd case from right is 1 -> Monday is selected 
3rd case from right is 1 -> Tuesday is selected 
4th case from right is 1 -> Wednesday is selected 
There are no more cases so no other day is selected 
関連する問題