2017-07-30 97 views
0

我想制作一个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! 
} 
+0

如何声明你的两个Cell类? - “NewsFeedImageTableViewViewCell”和“NewsFeedVideoTableViewViewCell”。在减少'tableView(_:cellForRowAt:)'中的一些代码之前,你是否减少了两个类中的重复代码? – OOPer

+0

'NewsFeedImageTableViewViewCell'和'NewsFeedVideoTableViewViewCell'继承'NewsFeedTableViewViewCell'。 –

+0

为什么不将'cell'声明为'NewsFeedTableViewViewCell',如果它具有所有通用属性呢? – OOPer

回答

2

要解决您的问题,请转换为正确的类型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

感谢您的帮助,真的很奇怪完整的解决方案 –

1

这个答案非常接近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() 
} 
+0

@cristanlika,我已经在我的Xcode中测试了我的代码。我的猜测填补了一些部分可能会导致错误,但从来没有这一行。请检查您是否确实输入了我的整个代码。 – OOPer

+0

是的正确的代码不给错误 –

+0

@cristanlika,感谢您的构想! – OOPer

相关问题