【问题标题】:Multi dimensional array populated from Firebase in Swift从 Swift 中的 Firebase 填充的多维数组
【发布时间】:2017-05-31 03:35:46
【问题描述】:

我正在尝试填充 Square 对象的二维数组 (UICollectionView):

class Square: NSObject {
     var sqrKey: String!
     var userId: String!

     init(SqrNumber: String, UserID: String) {
        self._sqrKey = SqrNumber
        self._userId = UserID        
     }

}

ViewController 是这样的:

class CustomCollectionViewController: UICollectionViewController {

     var ref: DatabaseReference!

     var squares = [Square]()

     override func viewDidLoad() {

         super.viewDidLoad()

         self.ref = Database.database().reference(withPath: "PlayerBoxes")

         handle = ref.child("11062").observe(.value, with: { snapshot in
             var items: [Square] = []

             for item in snapshot.children {
                let square = Square(snapshot: item as! DataSnapshot)
                items.append(square)
             }
             self.squares = items
             self.collectionView?.reloadData()
        })
     }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 5 
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 5
    }

    // Configure the cell
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCollectionViewCell

        cell.square = squares[(indexPath as NSIndexPath).item]

        cell.label.text = cell.square?._sqrKey

        return cell
    }
}

我遇到了两个问题/错误:

第一个问题比错误还多,即在执行 collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) 函数之前未完全完成 Firebase 读取,这会导致问题 fatal error: Index out of range 在线:cell.square = squares[(indexPath as NSIndexPath).item]

我该如何解决这个问题!?如何在继续之前强制 Firebase 读取完成!?

我的问题的第二部分是这个,我放置了虚拟数据以确保不会遇到上述错误/问题(通过硬编码 Square 对象数组)这确保不会遇到致命错误,并且我可以测试我的其余代码;但是当我这样做时,执行cell.label.text = cell.square?._sqrKey 时的结果输出我会为 UICollection 的每个部分中的每个项目获得相同的值!?

所以输出是:

0,1,2,3,4
0,1,2,3,4
0,1,2,3,4
0,1,2,3,4
0,1,2,3,4

我希望输出是

0,1,2,3,4,
5,6,7,8,9,
10,11,12,13,14
15,16,17,18,19
20,21,22,23,24

谁能帮我理解如何索引正确的_sqrKey 值!?

硬编码的 Square 对象是键值加 1,因此结果如上。

【问题讨论】:

    标签: arrays swift uicollectionview


    【解决方案1】:

    您从索引路径获得的 .item 是相对于该部分的。由于您的 squares 数组是一维的,因此您总是访问前 5 个元素。

    您可以通过考虑该部分来计算适当的索引:

    let index    = indexPath.section * 5 + indexPath.item
    cell.square  = squares[index]
    

    对于加载延迟导致运行时错误的问题,会在firebase请求完成之前显示您的集合时发生(例如,设置其数据源时)。要处理这个问题,您应该使用 squares 数组的实际大小而不是总是返回 5

    来响应 numberOfSections 和 numberOfItems
    return squares.count/5   // for number of sections
    
    return max(0,squares.count - section * 5)  // for items in section 
    

    或者,如果数组从未部分填充:

    return squares.count == 0 ? 0 : 5  // for number of sections
    
    return squares.count == 0 ? 0 : 5  // for items in section 
    

    [编辑]如果您希望正方形成为二维数组,则必须将其声明为数组数组,并相应地调整加载和使用。

    例如:

    var squares:[[Square]] = []
    
    ...
    
    var items: [[Square]] = []
    var row:[Square]      = []
    for item in snapshot.children 
    {
      let square = Square(snapshot: item as! DataSnapshot)
      row.append(square)
      if row.count == 5
      {
        items.append(row)
        row = []
      }
    }
    self.squares = items
    
    ...
    
    cell.square = squares[indexPath.section][indexPath.item]
    
    ...
    
    return squares.count // for sections
    
    ...
    
    return squares[section].count // for itms in section 
    

    【讨论】:

    • 如何将 Square 对象创建为二维数组!?
    • 你有 var items: [[Square]] = [ ],那应该是 var items: [Square] = [ ] 正确!?
    • 不,它确实是 [[Square]] 使其成为二维(即 [Square] 数组的 Array)。
    猜你喜欢
    • 2015-07-29
    • 1970-01-01
    • 2012-11-13
    • 2016-07-06
    • 2016-09-14
    • 1970-01-01
    • 2011-01-11
    相关资源
    最近更新 更多