【问题标题】:Custom TableViewCell using FirebaseUI and Swift使用 Firebase 和 Swift 自定义 TableView 单元
【发布时间】:2016-03-12 20:20:04
【问题描述】:

我目前正在尝试使用 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!'

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

【问题讨论】:

  • 什么是ManageTableViewCell?向我们展示您的 populateCellWithBlock 的外观。从文档中给我们提供示例并没有多大帮助
  • 我刚刚发现了@FruitAddict 的问题 - 已在答案中发布。

标签: swift firebase uitableview firebaseui


【解决方案1】:

我解决了。问题是您需要使用“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
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多