2017-05-15 12 views
0

私はいつでもcollectionviewを更新する必要があります。locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion)はサイクルを実行するので、beaconsの変数をcollectionviewに渡すと思いますグローバル変数を使用せずに。事前locationmanager(.. didrangbeacons)からcollectionview(... cellrangitacat)にデータを渡します

+0

ロケーションマネージャは別のクラスまたは同じビューコントローラにありますか? – user1046037

+0

同じviewController –

+0

同じView Controllerの場合は、View Controllerでプロパティを作成して設定できます。しかし、それは別のクラスでそれを維持し、ダンカンの答えに従うことがきれいです – user1046037

答えて

1

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) { 
    if !beacons.isEmpty { 
     //do things 
    } 
    else{ 
     //do other things 
    } 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! buttonBeaconsView 

    //i want to use beacons data here 


    //cell.backgroundColor = UIColor(white: 1, alpha: 0.8) 
    //cell.createButton() 
    //cell.layer.masksToBounds = true 
    return cell 
} 

おかげで、あなたのビューコントローラは、ロケーションマネージャの代理人として機能してください: ここではコードです。ビューコントローラに対してlocationManager(_:didRangeBeacons:in:)への呼び出しが行われます。その後、ビーコン情報の新しいリストを保持し、その内容を更新するようにコレクションビューに指示する変数(グローバル変数ではない)INSTANCEを更新することができます。

+0

、おかげで、明日私はしようとします –

+0

私は解決した、ありがとう!もう少し小さな質問があります:コレクションビューでは、ビーコン[i]のデータがあればビーコン[i]からi要素だけを取り出したいのですが、どうすればいいですか?私はビーコン[i]から単一の要素を取るためにindexPath.rowを使うべきですか? –

+0

indexPath.rowを使用して解決されました。 –

関連する問題