简体   繁体   中英

Value of type 'UITableViewCell' has no member

i want to make a cell variable and assign dequeueReusableCell for avoid duplication code. I have no idea how can I do that.

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()
}

If you see my code it only difference

let cell = tableView.dequeueReusableCell(withIdentifier: newsFeedVideoTableViewViewCellId, for: indexPath) as! NewsFeedImageTableViewViewCell 

And

let cell = tableView.dequeueReusableCell(withIdentifier: newsFeedVideoTableViewViewCellId, for: indexPath) as! NewsFeedVideoTableViewViewCell

Other lines of code are the same.

I am trying to declare variable this but it is not working:

let cell: UITableViewCell!

Value of type 'UITableViewCell' has no member 'imageTappedDelegate'

UPDATE:- added cell class definitions:

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

To resolve your issue, cast to the correct type before assigning the delegate. Whenever you reference cell, its of type UITableViewCell so those properties/methods on your custom subclasses do not exist.

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()
}

This answer is very near to the Josh Hamet's, so, I added some comments.

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM