繁体   English   中英

无法将“CustomProfileCell”类型的返回表达式转换为“UITableViewCell”类型

[英]Cannot convert return expression of type 'CustomProfileCell' to return type 'UITableViewCell'

这是我的代码

//
//  FirstViewController.swift
//  tabbed
//
//  Created by on 7/18/18.
//  Copyright © 2018 LR Web Design. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var profileTableView: UITableView!

    var profileArray = [Profile]()

    override func viewDidLoad() {
        super.viewDidLoad()

        profileTableView.delegate = self

        profileTableView.dataSource = self

        profileTableView.register(UINib(nibName: "ProfileCell",bundle: nil) , forCellReuseIdentifier: "customProfileCell")

        retrieveProfiles()

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileCell 
        cell.profileImage.image = UIImage(named: profileArray[indexPath.row].profileImage)
        cell.profileName.text = profileArray[indexPath.row].name
        cell.deviceCount.text = profileArray[indexPath.row].deviceCount 
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return profileArray.count
    } 
    func retrieveProfiles() {

        let p1 = Profile()
        p1.name = "John"
        p1.deviceCount = "10"
        p1.profileImage = "john"
        profileArray.append(p1)

        let p2 = Profile()
        p2.name = "Jane"
        p2.deviceCount = "5"
        p2.profileImage = "jane"
        profileArray.append(p2)

        let p3 = Profile()
        p3.name = "Andrew"
        p3.deviceCount = "4"
        p3.profileImage = "andrew"
        profileArray.append(p3)

        self.profileTableView.reloadData()

    }

}

我不断得到

无法将“CustomProfileCell”类型的返回表达式转换为“UITableViewCell”类型

在此处输入图片说明

由于您应该返回类型为UITableViewCell的自定义类,因此只有在以下情况下才会编译返回

class CustomProfileCell : UITableViewCell {
   // here outlets hooked to xib
}

好的,我的问题通过这种方式解决了

我在故事板中使用了 UICollectionView 但在课堂上我使用了 UITableView 的协议,所以当我改变它时,我的问题就解决了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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