現在カスタムTableViewを作成しケースごとに分けて表示しています。
各LabelにはAPIを叩いて取得したJsonデータが代入されています。
NavigationControllerを追加し他ところif let price:String = store![indexPath.row].price
と他のtime
,closedday
の部分で
Unexpectedly found nil while unwrapping an Optional value
とエラーが出ました。
NavigationControllerを追加する前は問題なく動いていました。
これはAPIを叩いて取得したJsonが読み込みが完了する前に強制アンラップしているのが原因でしょうか。
またその場合の対処はどうすればいいでしょうか。よろしくお願いします。
TableView
1 var store_id = "" 2 var store : [Store]? 3 var photoPath : [Store.photos]? 4 var tag : [Store.tags]? 5 6override func viewDidLoad() { 7 super.viewDidLoad() 8 9 10 //Collectiopn DetaSources 11 imageCollectionView.dataSource = self 12 imageCollectionView.delegate = self 13 14 tagCollectionView.dataSource = self 15 tagCollectionView.delegate = self 16 17 //UIView Shadow 18 let shadowPath = UIBezierPath(rect: UIView.bounds) 19 UIView.layer.masksToBounds = false 20 UIView.layer.shadowColor = UIColor.black.cgColor 21 UIView.layer.shadowOffset = .zero 22 UIView.layer.shadowOpacity = 0.2 23 UIView.layer.shadowPath = shadowPath.cgPath 24 25 //Request API 26 let url = URL(string: "http://localhost:8000/store/api?store_id=" + store_id) 27 let request = URLRequest(url: url!) 28 let session = URLSession.shared 29 30 let encoder: JSONEncoder = JSONEncoder() 31 encoder.dateEncodingStrategy = .iso8601 32 encoder.outputFormatting = .prettyPrinted 33 34 session.dataTask(with: request){(data, response, error)in if error == nil, 35 let data = data, 36 let response = response as? HTTPURLResponse{ 37 38 let decoder: JSONDecoder = JSONDecoder() 39 decoder.dateDecodingStrategy = .iso8601 40 do { 41 42 let json = try decoder.decode(Store.self, from: data) 43 44 if json != nil { 45 print("JSON is Nil") 46 }else{ 47 print(json) 48 } 49 self.store = [json] 50 self.photoPath = json.photos 51 self.tag = json.tags 52 53 if let imageURL = URL(string: "http://127.0.0.1:8000/photos/" + json.photos[0].path){ 54 DispatchQueue.global().async { 55 let data = try? Data(contentsOf: imageURL) 56 if let data = data { 57 let image = UIImage(data: data) 58 DispatchQueue.main.async { 59 self.mainImage.image = image 60 } 61 } 62 } 63 } 64 65 DispatchQueue.main.async { 66 self.nameLabel.text = json.name 67 self.locationLabel.text = json.location 68 69 } 70 71 } catch { 72 print("error:", error.localizedDescription) 73 74 } 75 76 } 77 78 }.resume() 79} 80 81 82func numberOfSections(in tableView: UITableView) -> Int { 83 return 3 84 } 85 86 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 87 switch section { 88 case 0: 89 return 1 90 case 1 : 91 return 1 92 case 2 : 93 return 1 94 default: 95 return 0 96 } 97 } 98 99func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 100 101 let cell = UITableViewCell() 102 103 switch indexPath.section { 104 case 0 : 105 //Price Cell 106 let priceCell = tableView.dequeueReusableCell(withIdentifier: "priceCell", for: indexPath) as? priceTableViewCell 107 108 if let price:String = store![indexPath.row].price{ 109 priceCell?.priceLabel.text! = price 110 }else{ 111 priceCell?.priceLabel.text! = "-" 112 } 113 return priceCell! 114 115 case 1 : 116 //timeCell 117 118 let timeCell = tableView.dequeueReusableCell(withIdentifier: "timeCell", for: indexPath) as? timeTableViewCell 119 120 if let time:String = store![indexPath.row].open_time{ 121 timeCell?.timeLabel.text! = time 122 }else{ 123 timeCell?.timeLabel.text! = "-" 124 } 125 return timeCell! 126 127 case 2 : 128 //closedayCell 129 let closedayCell = tableView.dequeueReusableCell(withIdentifier: "closedayCell", for: indexPath) as? closedayTableViewCell 130 131 if let closeday:String = store![indexPath.row].closed_day{ 132 closedayCell?.closedayLabel.text! = closeday 133 }else{ 134 closedayCell?.closedayLabel.text! = "-" 135 } 136 137 138 return closedayCell! 139 140 default : 141 print("Default Selected") 142 } 143 return cell 144 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/02 04:53
2018/10/02 05:03 編集
2018/10/02 05:07
2018/10/02 05:15
2018/10/02 05:20