arrays - 在 Swift 中从 Firebase 填充的多维数组

标签 arrays swift uicollectionview

我正在尝试填充方形对象的二维数组 (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 对象数组)这确保不会遇到 fatal error 并且我可以测试我的其余代码;但是当我这样做时,执行 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,因此得到上面的结果。

最佳答案

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

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

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

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

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 

关于arrays - 在 Swift 中从 Firebase 填充的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44275183/

相关文章:

ios - Objective-C -NSMutablearray 特定索引存储在另一个数组中

ios - 如何使用 RxSwift disposeBag 防止 UITableViewCell 中重复的 UIButton 点击

ios - 将 subview 添加到占据整个单元格框架的 UICollectionViewCell

ios - 使用 UICollectionView 的按钮网格?

ios - 确保只有 1 个单元格在 UICollectionView 中具有事件状态

c - 十六进制数组转浮点型

c - 在C中解析和拆分来自txt文件的字符串

swift - 垂直和水平滚动collectionView选择单元格问题

json - 如何转换 JSON 以在 Swift 中使用 CLLocationCoordinate2D

python - 如何使用 PANDAS/Python 将矩阵转换为列数组