前提・実現したいこと
あけましておめでとうございます
現在SwiftでToDoアプリを開発しており、ユーザーが追加したToDoをTableView Cellに表示されるようになっています。
そして対象のセルを左から右にスワイプすると「Share」が表示され、Shareを押すとそのセルのデータをUIActivityViewControllerで共有できるようにしたいのですが、最初自分でやってみるとスワイプしたセル以外の内容も共有されてしまいます。
そこで自分でなんとかやってみたのですがうまくいきませんでした
発生している問題・エラーメッセージ
Thread 1: signal SIGABRT
該当のソースコード
Swift
1//TodoTableViewController 2import UIKit 3 4class TodoTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 @IBOutlet var tableView: UITableView! 7 8 var resultArray = [String]() 9 10 var str = UserDefaults.standard.array(forKey: "array") 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 tableView.delegate = self 16 tableView.dataSource = self 17 18 } 19 20 override func viewWillAppear(_ animated: Bool) { 21 super.viewWillAppear(animated) 22 23 if UserDefaults.standard.object(forKey: "array") != nil { 24 25 resultArray = UserDefaults.standard.object(forKey: "array") as! [String] 26 } 27 28 tableView.reloadData() 29 30 } 31 32 func numberOfSections(in tableView: UITableView) -> Int { 33 // #warning Incomplete implementation, return the number of sections 34 return 1 35 } 36 37 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 38 // #warning Incomplete implementation, return the number of rows 39 return resultArray.count 40 } 41 42 43 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 44 45 //let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 46 let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell") 47 48 cell.textLabel?.text = resultArray[indexPath.row] 49 50 return cell 51 } 52 53 54 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 55 if editingStyle == .delete { 56 57 resultArray.remove(at: indexPath.row) 58 59 UserDefaults.standard.set(resultArray, forKey: "array") 60 61 tableView.reloadData() 62 } 63 64 } 65 66 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 67 cell.contentView.backgroundColor = UIColor.black 68 cell.textLabel?.textColor = UIColor.white 69 } 70 71 72 //ここからは応用 73 74 func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 75 let shareAction = UIContextualAction(style: .normal, 76 title: "Share") { (action, view, completionHandler) in 77 // 何らかのアクション(処理)を実行 78 //let shareText = self.str(indexPath?.row) 79 //let shareText = self.resultArray(indexPath.row) 80 81 let shareText: [String] = self.str?[indexPath.row] as! [String] 82 83 let avc = UIActivityViewController(activityItems: shareText, applicationActivities: nil) 84 self.present(avc, animated: true, completion: nil) 85 //completionHandler を呼ぶ 86 completionHandler(true) 87 } 88 shareAction.backgroundColor = .cyan 89 let configuration = UISwipeActionsConfiguration(actions: [shareAction]) 90 return configuration 91 } 92 93} 94 95 96 97 98 99 100 101//ここからAddViewController 102import UIKit 103 104class AddViewController: UIViewController { 105 106 @IBOutlet weak var textField: UITextField! 107 108 var array = [String]() 109 110 override func viewDidLoad() { 111 super.viewDidLoad() 112 113 114 // Do any additional setup after loading the view. 115 } 116 117 @IBAction func add(_ sender: Any) { 118 119 if UserDefaults.standard.object(forKey: "array") != nil { 120 121 array = UserDefaults.standard.object(forKey: "array") as! [String] 122 } 123 124 array.append(textField.text!) 125 126 UserDefaults.standard.set(array, forKey: "array") 127 128 self.navigationController?.popViewController(animated: true) 129 130 } 131 132 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 133 self.view.endEditing(true) 134 } 135 136 137 138} 139 140
試したこと
UserDefaultsやresultArrayについて見直しました
またThread 1: signal SIGABRT以外のエラーも出ていたのですが(Thread 1: Fatal error: Index out of rangeなど)、それらのエラーも解決できませんでした。
補足情報(FW/ツールのバージョンなど)
Swift4.2.1
Xcode10.1
Deployment Target 11.0
原因がわかる方がいらっしゃいましたら、ご教授ください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/01 13:59
2019/01/01 14:11