0

我目前正在尝试使用 Firebase UI 来使用自定义单元格填充 UITableView。不幸的是证明很困难,并且以前建议的修复方法不起作用。

文档(https://github.com/firebase/FirebaseUI-iOS#using-storyboards-and-prototype-cells)演示了这一点:

self.dataSource = FirebaseCollectionViewDataSource(ref: firebaseRef cellClass: YourCustomClass.self cellReuseIdentifier: @"<YOUR-REUSE-IDENTIFIER>" view: self.collectionView)

self.dataSource.populateCellWithBlock { (cell: YourCustomClass, obj: NSObject) -> Void in
  // Populate cell as you see fit
  cell.customView = customView;
}

self.collectionView.dataSource = self.dataSource;

复制这个并插入我自己的 TableViewCellClass 会产生这个错误:

无法将类型“(ManageTableViewCell,NSObject)-> Void”的值转换为预期的参数类型“(UITableViewCell,NSObject)-> Void”

然后我看到了这个问题线程(https://github.com/firebase/FirebaseUI-iOS/issues/16),其中一个 Firebase 团队说强制演员会像这样工作:

 self.dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
  // Populate cell as you see fit, like as below
  var customCell = cell as! CustomTableViewCell;
  let snapshot = obj as! FDataSnapshot; // danger this can be null on deletion!
}

但是,这会立即产生错误:

无法从“UITableViewCell”向下转换为更可选的类型“ManageTableViewCell!”

有没有人对如何做到这一点有任何建议。这似乎是一个没有真正解决办法的常见问题。

4

1 回答 1

2

我解决了。问题是您需要使用“prototypeReuseIdentifier”而不是“cellReuseIdentifier”。

我的工作代码如下:

    dataSource = FirebaseTableViewDataSource(query: firebaseQuery, prototypeReuseIdentifier: "textCell", view: self.tableView)

    dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in

        var customCell = cell as! ManageTableViewCell

        let snap = obj as! FDataSnapshot

        customCell.label99.text = snap.key as String
    }
于 2016-03-12T20:43:32.973 回答