将数据从 locationmanager(..didrangbeacons) 传递到 collectionview(...cellforitemat)

Pass data from locationmanager(..didrangbeacons) to collectionview(...cellforitemat)

每次 locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) 执行一个循环时我都必须更新 collectionview,所以我想将 beacons 变量从它传递到 collectionview,我如何在不使用全局变量的情况下做到这一点。 这是代码:

 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 变量(不是全局变量)以保存新的信标信息列表并告诉集合视图更新它的内容。