Swift:无法将类型“UITableViewCell”的值转换为自定义单元类 [英] Swift: Could not cast value of type 'UITableViewCell' to a custom cell class
问题描述
不能将'UITableViewCell'(0x10a73f9f0)类型的值转换为'EFRideSharing.RideTableViewCell'(0x108117f20)。
dataSource = FirebaseTableViewDataSource(ref:ref,cellReuseIdentifier:rideCell,view:self.ridesTableView)
dataSource.populateCellWithBlock
(cell,snapshot) - >无效
让tvc:RideTableViewCell = cell as! RideTableViewCell
让快照:FDataSnapshot =快照为! FDataSnapshot
tvc.toLabel.text = snapshot.value [time] as?字符串
}
任何想法如何使它工作?
更新:额外的代码片段
pre $ class $ RideTableViewCell:UITableViewCell {
@IBOutlet weak var timeLabel:UILabel!
@IBOutlet weak var toLabel:UILabel!
@IBOutlet weak var fromLabel:UILabel!
$ b覆盖func awakeFromNib(){
super.awakeFromNib()
//初始化代码
}
override func setSelected(selected:Bool,animated:Bool){
super.setSelected(selected,animated:animated)
//配置所选状态的视图
}
}
FirebaseUI-iOS的作者。看起来问题是我们返回一个 UITableViewCell
而不是 RideTableViewCell code>,因为由
属性没有设置。 FirebaseTableViewDataSource
返回的单元格的默认类是 UITableViewCell
code> cellClass
您可以使用包含
dataSource = FirebaseTableViewDataSource(ref:ref,cellClass:RideTableViewCell.self ,cellReuseIdentifi呃:rideCell,view:self.ridesTableView)
这将允许我们返回适当的类型对象的版本给你。另外,为了解决在群体上静态地知道单元格类型的问题,w可以做到以下几点。
在Objective-C中,这非常简单,因为我们可以给块中的属性一个不同的类型:
$ b $ pre $ $ $ $ $ $ $ {
//根据您的需要填充单元格
}];
因为我们使用 __ kind of UITableViewCell
作为参数如果可用的话( id
不存在,所以在XCode 7之前),这个行为在任何版本的XCode中都可以正常工作,因为XCode会接受是的,这个类是子类(> = XCode 7)或者如果它是 id
我仍然可以发送消息给它,所以我会允许它(< = XCode 6.4) 。
Swift是另一回事。虽然你认为你应该能够做到:
$ b $ pre $ data $ dataSource.populateCellWithBlock {(cell:YourCustomClass,snapshot:FDataSnapshot) - >无效在
/ /填充细胞,因为你看到适合
}
合理的,预制版的细胞。也就是说,我一直得到错误,说方法签名不匹配比较 AnyObject
和 YourCustomClass
和把它写成一个苹果问题,用 __ kindof
,这是我所知道的记录最少的功能之一。如果任何人有任何想法,为什么这可能是这种情况,或知道更好的文件,我很想听听这是从哪里来的(或者如果它固定在通用版本的XCode 7)。 $ b
I'm working on iOS app, which needs to pull the data from Firebase and display in a table view. To give it a custom design, I've created a custom table view cell class. When I'm trying to display the data that firebase returns in "populateCellWithBlock" method, the app crushes, displaying this error:
Could not cast value of type 'UITableViewCell' (0x10a73f9f0) to 'EFRideSharing.RideTableViewCell' (0x108117f20).
dataSource = FirebaseTableViewDataSource(ref: ref, cellReuseIdentifier: "rideCell", view: self.ridesTableView)
dataSource.populateCellWithBlock
{ (cell,snapshot) -> Void in
let tvc: RideTableViewCell = cell as! RideTableViewCell
let snapshot: FDataSnapshot = snapshot as! FDataSnapshot
tvc.toLabel.text = snapshot.value["time"] as? String
}
Any ideas how to make it work?
Update: Additional pieces of code
class RideTableViewCell: UITableViewCell {
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var toLabel: UILabel!
@IBOutlet weak var fromLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Author of FirebaseUI-iOS here.
Looks like the issue is that we're returning a UITableViewCell
rather than a RideTableViewCell
due to the fact that the default class of the cell returned by FirebaseTableViewDataSource
is UITableViewCell
when the cellClass
property isn't set.
You can use the version of the constructor that contains cellClass
to solve this problem:
dataSource = FirebaseTableViewDataSource(ref: ref, cellClass: RideTableViewCell.self, cellReuseIdentifier: "rideCell", view: self.ridesTableView)
This will allow us to return the appropriately cast versions of the object to you. Also, to address issues of knowing the type of the cell statically on population, w can do the following.
In Objective-C this is really easy, as we can just give the properties in the block a different type:
[self.dataSource populateCellWithBlock:^(YourCustomCellClass *cell, FDataSnapshot *snap) {
// Populate cell as you see fit
}];
Because we use __kindof UITableViewCell
as the argument where available (id
when not, so pre XCode 7), this behavior works as intended in any version of XCode, since XCode will either accept "Yes, this class is a subclass of UITableViewCell" (>= XCode 7) or "If it's id
I can still send a message to it, so I'll allow it" (<= XCode 6.4).
Swift is a different story. While you'd think that you should be able to do:
dataSource.populateCellWithBlock{ (cell: YourCustomClass, snapshot: FDataSnapshot) -> Void in
// Populate cell as you see fit
}
And get reasonable, pre-cast versions of the cells. That said, I've consistently gotten errors thrown saying that the method signatures don't match comparing AnyObject
to YourCustomClass
and chalked it up as an Apple issue with __kindof
, which is one of the least documented features I know of. If anyone has any thoughts on why this might be the case, or knows of better documentation, I'd love to hear where this is coming from (or if it's fixed in the GM release of XCode 7).
这篇关于Swift:无法将类型“UITableViewCell”的值转换为自定义单元类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!