見出し画像

【Swift】AppleシリコンMacbook上で動作させると一部処理が行われないメソッドが存在するかもしれない

実例

extension ListViewController: TableViewReorderDelegate {
   func tableView(_ tableView: UITableView, reorderRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { // Update data model let
       let deck = self.listViewModel.deckList[sourceIndexPath.row]
       self.listViewModel.deckList.remove(at: sourceIndexPath.row)
       self.listViewModel.deckList.insert(deck, at: destinationIndexPath.row)

   }
   
   // 移動完了後の処理
   func tableViewDidFinishReordering(_ tableView: UITableView, from initialSourceIndexPath: IndexPath, to finalDestinationIndexPath: IndexPath) {
       self.tableView.reloadData()
   }
   
}

My Mac (Designed for iPad)、AppStoreからのダウンロードにて動作を確認しましたが、上記デリゲートメソッドが動作しませんでした。

こちらはSwiftReorderというライブラリなのですが、なぜかこのメソッドは動作しませんでした。

故にiPad/iPhoneシミュレーターでは上手く行っている挙動がMacでは挙動が異なりました。

iPad/iPhoneで動作しているアプリの確認にはMac(ビルド)を使うのは悪手なような気がします。

自分の発生事象に対するトラブルシューティング

    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
       // AppleシリコンはSwiftReorderDelegateが動作しないのでこちらで処理を代行する
       if UIDevice.current.userInterfaceIdiom == .pad {
           if (!UIDevice.current.name.contains("iPad")) {
               // Appleシリコン(Macbook)の時の分岐
               self.listViewModel.deckList.replaceIndexElement(targetIndex: sourceIndexPath.row, swapIndex: destinationIndexPath.row)
               self.tableView.reloadData()
               self.listViewModel.saveOriginalDataArray()
           }
       }
   }

代わりにmoveRowAt(:)が呼び出されていたので、Appleシリコンではこちらで同様の処理を行うことにしました。
iPadでは上記処理はそもそも呼ばれていませんが、ifで一応くくっています。


この記事が気に入ったらサポートをしてみませんか?