###前提・実現したいこと
時間管理アプリtogglのようにUITableViewCellにタイマーを設置しょうとしております。
現状タイマー設置自体はできたのですが、以下の現状を解決できず悩んでおります。
###発生している問題・エラーメッセージ
タイマーの描画更新にreloadData利用しているため、reloadDataのタイミングでスワイプ操作がリセットされてしまう。
タイマー(UITableView)の更新とスワイプ操作を共存させるには、どのようなやり方がありますでしょうか。
ご教授頂けますと幸いです。
スワイプはライブラリ:MCSwipeTableViewCellを利用しています。
以下キャプチャのようにスワイプを途中でとめているとタイマー更新のタイミングでスワイプ操作がリセットされてしまいます。
###該当のソースコード
swift
1 2 var todoNames = ["task01","task02","task03"] 3 var todoprojects = ["projectA","projectB",""] 4 var todoContexts = ["contextA","","contextB"] 5 var todoCellTimeRecords = ["","",""] 6 7func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 8 let cell = tableView.dequeueReusableCell(withIdentifier: "todoCell", for: indexPath) as! todoListTableViewCell 9 cell.todoCellName.text = todoNames[indexPath.row] 10 cell.todoCellProjectName.text = todoprojects[indexPath.row] 11 cell.todoCellContext.text = todoContexts[indexPath.row] 12 cell.todoCellTimer.text = todoCellTimeRecords[indexPath.row] 13 14 switch timerRunning { 15 case true: 16 cell.setSwipeGestureWith(UIImageView(image: UIImage(named: "timer")), color: UIColor.red, mode: .switch, state: .state3, completionBlock: { [weak self] (cell, state, mode) in 17 self?.timerRunning = false 18 self?.timer.invalidate() 19 self?.countNum = 0 20 self?.todoCellTimeRecords[indexPath.row] = "" 21 self?.todoList.reloadData() 22 }) 23 default: 24 cell.setSwipeGestureWith(UIImageView(image: UIImage(named: "timer")), color: UIColor.blue, mode: .switch, state: .state3, completionBlock: { [weak self] (cell, state, mode) in 25 26 if let cell = cell, let indexPath = tableView.indexPath(for: cell) { 27 self?.startTimer(indexPath.row) 28 self?.todoList.reloadData() 29 } 30 }) 31 } 32 return cell 33 } 34 35 36 weak var timer: Timer! 37 var countNum = 0 38 var timerRunning = false 39 40func startTimer(_ indexPath:Int){ 41 timer = Timer.scheduledTimer( 42 timeInterval: 1, 43 target: self, 44 selector: #selector(self.timerCounter), 45 userInfo: indexPath, 46 repeats: true) 47 timerRunning = true 48 } 49 50 var cellNum = 0 51 var timeRecord = "00:00:00" 52 func timerCounter(_ timer:Timer!){ 53 countNum += 1 54 let indexPath = timer.userInfo as! Int 55 cellNum = indexPath <= todoNames.count ? indexPath : cellNum 56 let hour = countNum / 3600 < 10 ? "0\(String(countNum / 3600))" : String(countNum / 3600) 57 let minute = countNum % 3600 / 60 < 10 ? "0\(String(countNum % 3600 / 60))" : String(countNum % 3600 / 60) 58 let second = countNum % 3600 % 60 < 10 ? "0\(String(countNum % 3600 % 60))" : String(countNum % 3600 % 60) 59 timeRecord = "\(hour):\(minute):\(second)" 60 todoCellTimeRecords[cellNum] = timeRecord 61 todoList.reloadData() 62 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/15 23:55 編集