質問編集履歴

1

一度ご指摘の通りectensionをやめ、グローバル変数をやめた後のコードです。

2019/05/09 13:26

投稿

Tajiko
Tajiko

スコア12

test CHANGED
File without changes
test CHANGED
@@ -292,6 +292,210 @@
292
292
 
293
293
 
294
294
 
295
+ ```swift
296
+
297
+ 修正後
298
+
299
+ import UIKit
300
+
301
+ import MapKit
302
+
303
+
304
+
305
+ class ViewController: UIViewController, UISearchBarDelegate, MKMapViewDelegate, UITableViewDelegate, UITableViewDataSource,MKLocalSearchCompleterDelegate {
306
+
307
+
308
+
309
+ @IBOutlet weak var mapView: MKMapView!
310
+
311
+ @IBOutlet weak var searchBar: UISearchBar!
312
+
313
+ @IBOutlet weak var tableView: UITableView!
314
+
315
+ var locationManager: CLLocationManager?
316
+
317
+ var annotationArray: [CustomAnnotation] = []
318
+
319
+ var completer = MKLocalSearchCompleter()
320
+
321
+
322
+
323
+ override func viewDidLoad() {
324
+
325
+ super.viewDidLoad()
326
+
327
+ //省略
328
+
329
+
330
+
331
+ searchBar.delegate = self
332
+
333
+ tableView.delegate = self
334
+
335
+ tableView.dataSource = self
336
+
337
+ tableView.isHidden = true
338
+
339
+ completer.delegate = self
340
+
341
+ completer.filterType = .locationsOnly
342
+
343
+ }
344
+
345
+
346
+
347
+
348
+
349
+
350
+
351
+ func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
352
+
353
+
354
+
355
+ if searchBar.text != "" {
356
+
357
+ tableView.isHidden = false
358
+
359
+ } else {
360
+
361
+ tableView.isHidden = true
362
+
363
+ }
364
+
365
+
366
+
367
+ searchBar.setShowsCancelButton(true, animated: true)
368
+
369
+ }
370
+
371
+
372
+
373
+ func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
374
+
375
+ //キーボードを閉じる
376
+
377
+ searchBar.resignFirstResponder()
378
+
379
+ //キャンセルボタンの表示
380
+
381
+ searchBar.setShowsCancelButton(false, animated: true)
382
+
383
+
384
+
385
+ //入力された文字を取り出す
386
+
387
+ if let searchKey = searchBar.text {
388
+
389
+ //入力された文字をデバッグに表示
390
+
391
+ print(searchKey)
392
+
393
+
394
+
395
+ //CLGeocoderインスタンスを取得
396
+
397
+ let geocoder = CLGeocoder()
398
+
399
+ //入力された文字列から位置情報を取得してアノテーションで表示する処理。省略。
400
+
401
+ }
402
+
403
+ }
404
+
405
+
406
+
407
+ //入力に変更があった際に呼び出されるメソッド.
408
+
409
+ func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
410
+
411
+ guard let text = searchBar.text else { return false }
412
+
413
+ completer.queryFragment = text
414
+
415
+ return true
416
+
417
+ }
418
+
419
+
420
+
421
+ // キャンセルボタンタップ時に呼び出されるメソッド.
422
+
423
+ func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
424
+
425
+ searchBar.setShowsCancelButton(false, animated: true)
426
+
427
+ tableView.isHidden = true
428
+
429
+ }
430
+
431
+
432
+
433
+ //tableViewが何行か決めるメソッド
434
+
435
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
436
+
437
+ return completer.results.count
438
+
439
+ }
440
+
441
+
442
+
443
+ //セルの内容を設定するメソッド
444
+
445
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
446
+
447
+ let identifier = "Cell"
448
+
449
+ var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
450
+
451
+
452
+
453
+ if (cell == nil) {
454
+
455
+ cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
456
+
457
+ }
458
+
459
+
460
+
461
+ cell?.textLabel?.text = completer.results[indexPath.row].title
462
+
463
+ return cell!
464
+
465
+ }
466
+
467
+
468
+
469
+ func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
470
+
471
+ tableView.estimatedRowHeight = 20
472
+
473
+ return UITableView.automaticDimension
474
+
475
+ }
476
+
477
+
478
+
479
+ // セルを選択したときのメソッド
480
+
481
+ private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
482
+
483
+
484
+
485
+ }
486
+
487
+
488
+
489
+ }
490
+
491
+
492
+
493
+
494
+
495
+ ```
496
+
497
+
498
+
295
499
  ### 試したこと
296
500
 
297
501