类型'UITableViewCell'的值没有成员
问题描述:
我想制作一个cell
变量,并指定dequeueReusableCell
以避免重复代码。我不知道我该怎么做。类型'UITableViewCell'的值没有成员
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell : UITableViewCell!
if let url = media.url {
if Helper.isImageType(url: url)
{
cell = tableView.dequeueReusableCell(withIdentifier: newsFeedImageTableViewViewCellId, for: indexPath) as! NewsFeedImageTableViewViewCell
cell.imageTappedDelegate = self
}else
{
cell = tableView.dequeueReusableCell(withIdentifier: newsFeedVideoTableViewViewCellId, for: indexPath) as! NewsFeedVideoTableViewViewCell
cell.videoTappedDelegate = self
}
cell.linkTappedDelegate = self
cell.backgroundColor = UIColor(rgb: 0xF2F2F2)
cell.isAccessibilityElement = true
cell.selectionStyle = .none
tableViewIndex = indexPath
if let _states = states?[indexPath.section]{
cell.state = _states[indexPath.row]
}
return cell
}
return UITableViewCell()
}
如果你看到我的代码,它唯一的区别
let cell = tableView.dequeueReusableCell(withIdentifier: newsFeedVideoTableViewViewCellId, for: indexPath) as! NewsFeedImageTableViewViewCell
而且
let cell = tableView.dequeueReusableCell(withIdentifier: newsFeedVideoTableViewViewCellId, for: indexPath) as! NewsFeedVideoTableViewViewCell
的代码,其他线路是相同的。
我想声明变量这一点,但它不工作:
let cell: UITableViewCell!
类型的值 '的UITableViewCell' 没有成员 'imageTappedDelegate'
更新: - 增加细胞类定义:
class NewsFeedTableViewViewCell : BaseTableViewCell{
var statisticsSlidingCellId = "statisticsSlidingCellId"
var linkTappedDelegate : LinkTappedDelegate!
var state : State?{
didSet{
}
}
}
class NewsFeedImageTableViewViewCell: NewsFeedTableViewViewCell{
var imageTappedDelegate : ImageTappedDelegate!
}
class NewsFeedVideoTableViewViewCell : NewsFeedTableViewViewCell{
var videoTappedDelegate : VideoTappedDelegate!
}
答
要解决您的问题,请转换为正确的类型pe分配代表之前。每当你引用单元格时,它的类型为UITableViewCell,所以你的自定义子类中的那些属性/方法不存在。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell : NewsFeedTableViewViewCell!
if let url = media.url {
if Helper.isImageType(url: url)
{
cell = tableView.dequeueReusableCell(withIdentifier: newsFeedImageTableViewViewCellId, for: indexPath) as! NewsFeedImageTableViewViewCell
(cell as ! NewsFeedImageTableViewCell).imageTappedDelegate = self
}else
{
cell = tableView.dequeueReusableCell(withIdentifier: newsFeedVideoTableViewViewCellId, for: indexPath) as! NewsFeedVideoTableViewViewCell
(cell as! NewsFeedVideoTableViewCell).videoTappedDelegate = self
}
cell.linkTappedDelegate = self
cell.backgroundColor = UIColor(rgb: 0xF2F2F2)
cell.isAccessibilityElement = true
cell.selectionStyle = .none
tableViewIndex = indexPath
if let _states = states?[indexPath.section]{
cell.state = _states[indexPath.row]
}
return cell
}
return UITableViewCell()
}
+0
感谢您的帮助,真的很奇怪完整的解决方案 –
答
这个答案非常接近Josh Hamet's,所以我添加了一些评论。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let url = media.url {
let cell: NewsFeedTableViewViewCell //<- common class
if Helper.isImageType(url: url) {
let imageCell = tableView.dequeueReusableCell(withIdentifier: newsFeedImageTableViewViewCellId, for: indexPath) as! NewsFeedImageTableViewViewCell
//When accessing `imageTappedDelegate`, the type of the cell needs to be `NewsFeedImageTableViewViewCell`.
imageCell.imageTappedDelegate = self
cell = imageCell
} else {
let videoCell = tableView.dequeueReusableCell(withIdentifier: newsFeedVideoTableViewViewCellId, for: indexPath) as! NewsFeedVideoTableViewViewCell
//When accessing `videoTappedDelegate`, the type of the cell needs to be `NewsFeedVideoTableViewViewCell`.
videoCell.videoTappedDelegate = self
cell = videoCell
}
//When accessing common properties, the type of the cell can be `NewsFeedTableViewViewCell`.
cell.linkTappedDelegate = self
cell.backgroundColor = UIColor(rgb: 0xF2F2F2)
cell.isAccessibilityElement = true
cell.selectionStyle = .none
tableViewIndex = indexPath
if let _states = states?[indexPath.section]{
cell.state = _states[indexPath.row]
}
return cell
}
return UITableViewCell()
}
如何声明你的两个Cell类? - “NewsFeedImageTableViewViewCell”和“NewsFeedVideoTableViewViewCell”。在减少'tableView(_:cellForRowAt:)'中的一些代码之前,你是否减少了两个类中的重复代码? – OOPer
'NewsFeedImageTableViewViewCell'和'NewsFeedVideoTableViewViewCell'继承'NewsFeedTableViewViewCell'。 –
为什么不将'cell'声明为'NewsFeedTableViewViewCell',如果它具有所有通用属性呢? – OOPer