【问题标题】:Pass data from locationmanager(..didrangbeacons) to collectionview(...cellforitemat)将数据从 locationmanager(..didrangbeacons) 传递到 collectionview(...cellforitemat)
【发布时间】:2017-10-14 20:55:50
【问题描述】:

每次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
}

提前致谢

【问题讨论】:

  • 位置管理器是在一个单独的类中还是在同一个视图控制器中?
  • 同一个viewController
  • 如果是同一个视图控制器,您可以在视图控制器中创建一个属性并设置它。但是,将它放在不同的类中并遵循邓肯的回答会更干净

标签: ios swift xcode uicollectionview cllocationmanager


【解决方案1】:

让您的视图控制器充当位置管理器的委托。对locationManager(_:didRangeBeacons:in:) 的调用将发送到您的视图控制器。然后它可以更新一个 INSTANCE 变量(不是全局变量)来保存新的信标信息列表并告诉集合视图更新它的内容。

【讨论】:

  • 好的,我解决了,谢谢!我还有一个小问题:在收集视图中,一旦我拥有信标 [i] 数据,我只想从信标 [i] 中获取 i 元素,我该怎么做?我应该使用 indexPath.row 从 beacons[i] 中获取单个元素吗?
  • 使用 indexPath.row 解决
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多