2017-11-15 4 views
0

私は最終的な結果のジョブリスト配列を外枠で使用したいと考えています。私はジョブリスト配列を使ってテーブルビューをセットアップしたいからです。 。swift3サイドクロージャーで配列を取得

let geoCoder = CLGeocoder() 
geoCoder.geocodeAddressString(address) { (placemarks, error) in 
    if error == nil && (placemarks?.count)! > 0 { 

     let location2 = placemarks?[0].location 

     if let location1 = self.locationManager?.location { 
      let distanceInMeters = location1.distance(from: location2!) 
      let IntDis = Int(distanceInMeters) 
      //print(IntDis) 

      if IntDis < 40000 { 

       //print(address) 

       if let activityid  = infoDictionary["ActivityID"] {self.newJob.ActivityID=activityid} 
       if let companyname  = infoDictionary["CompanyName"] {self.newJob.CompanyName=companyname} 
       if let quantity   = infoDictionary["Quantity"] {self.newJob.Quantity=quantity} 
       if let coupontitle  = infoDictionary["Title"] {self.newJob.CouponTitle=coupontitle} 
       if let couponterms  = infoDictionary["Terms"] {self.newJob.CouponTerms=couponterms} 
       if let expirdate  = infoDictionary["ExpirDate"] {self.newJob.ExpirDate=expirdate} 
       if let contactperson = infoDictionary["ContactPerson"] {self.newJob.ContactPerson=contactperson} 
       if let tel    = infoDictionary["TEL"] {self.newJob.TEL=tel} 

       self.joblist.append(self.newJob) 
       //print(self.joblist) 
       //self.tableView.reloadData() 

      } 
     } 
    } 
} 

答えて

0

次の2つの方法でこれを行うことができます:

  1. を、それが複数の場所
  2. からアクセスできるように、以前に通過する間に新しい関数を呼び出すコールバックを使用しjoblistは変数グローバル作ります収集されたデータ。これは、元の「データ取得」プロセスが遅延を伴うネットワークタスクである場合に特に有用です。

グローバル変数:コールバック(次の関数に自分自身に合格しない簡易版)を使用して

let geoCoder = CLGeocoder() 
var jobList = [AnyClass]() //make a global array 
geoCoder.geocodeAddressString(address) { (placemarks, error) in 
    ...filtering code ... 
    joblist.append(self.newJob) 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
    cell.textLabel = jobList[indexPath.row].ActivityID //or something else with the data 
} 

geoCoder.geocodeAddressString(address) { (placemarks, error) in 
    ...filtering code ... 
    doTableViewStuff(data: joblist) 
} 

func doTableViewStuff(data: [Jobs]) { //or whatever type of data 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
     cell.textLabel = data[indexPath.row].ActivityID //or something else with the data 
    } 
} 
+0

あなたが言ったことによります。あなたは、レコードがテーブルビューに追加されるたびに、Tableviewが開始時に空のレコードを表示し、テーブルビューを再ロードし続けることを意味しますか? –

+0

私はそうだと思います、上に掲示された私の答えはあなたのために働くのですか? – Eric

+0

リロードを維持することは良い考えではありません。なぜなら、行の数が10を超える可能性があるからです。これは10回以上リロードし続けます。これが私のやり方です。そのため、配列の更新が完了した後にテーブルビューをリロードするように改善したいのです。 –

関連する問題