Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 类型为'的值;UITableViewCell';没有会员_Ios_Swift_Uitableview - Fatal编程技术网

Ios 类型为'的值;UITableViewCell';没有会员

Ios 类型为'的值;UITableViewCell';没有会员,ios,swift,uitableview,Ios,Swift,Uitableview,我想制作一个cell变量并分配dequeueReusableCell,以避免代码重复。我不知道我该怎么做 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell : UITableViewCell! if let url = media.url { if Helper.isImag

我想制作一个
cell
变量并分配
dequeueReusableCell
,以避免代码重复。我不知道我该怎么做

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: 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!
}

若要解决问题,请在分配代理之前强制转换为正确的类型。无论何时引用单元格,其类型都是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()
}

若要解决问题,请在分配代理之前强制转换为正确的类型。无论何时引用单元格,其类型都是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()
}

这个答案与Josh Hamet的答案非常接近,因此,我添加了一些评论

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()
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
如果let url=media.url{

let cell:NewsFeedTableViewCell/这个答案与Josh Hamet的答案非常接近,因此,我添加了一些评论

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()
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
如果let url=media.url{

let cell:NewsFeedTableViewCell//如何声明这两个单元格类?--
NewsFeedImageTableViewCell
NewsFeedVideoTableViewCell
。在减少
表视图中的一些代码之前(\uU:cellForRowAt:)
,您是否减少了这两个类中的重复代码?
NewsFeedImageTableViewCell
NewsFeedVideoTableViewCell
继承了
NewsFeedTableViewCell
。如果您的
单元格中有所有公共属性,为什么不将其声明为
NewsFeedTableViewCell
ell.imageTappedDelegate=self
cell.videoTappedDelegate=self
不一样,有些for属性不同。在尝试分配属性之前,您需要将单元格强制转换为具有这些属性的任何类型。如果所有属性都在一个子类中,则将表单元格初始化为该子类而不是基类UITableViewCell如何声明这两个单元格类?--
NewsFeedImageTableViewCell
NewsFeedVideoTableViewCell
。在减少
表视图中的某些代码之前(\uu:cellForRowAt:)
,您是否减少了这两个类中的重复代码?
NewsFeedImageTableViewCell
NewsFeedVideoTableViewCell
继承了
NewsFeedTableViewCell
。如果您的
单元格中有所有公共属性,为什么不将其声明为
NewsFeedTableViewCell
ell.imageTappedDelegate=self
cell.videoTappedDelegate=self
不一样,有些for属性不同。在尝试分配属性之前,您需要将单元格强制转换为具有这些属性的任何类型。如果所有属性都在一个子类中,则将表单元格初始化为该子类而不是基类 UITableViewCell@cristanlika,我已经在我的Xcode中测试了我的代码。由我的猜测填充的某些部分可能会导致错误,但不会导致该行。请检查您是否确实正确输入了我的全部代码。@cristanlika,我已经在我的Xcode中测试了我的代码。由我的猜测填充的某些部分可能会导致错误,但不会导致该行。请检查您是否确实输入了该行我的全部代码都是正确的。