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

比布兹

我的代码:

//
//  ForumViewController.swift
//  Kode4Kids
//
//  Created by Caleb Clegg on 17/06/2020.
//  Copyright © 2020 Group9. All rights reserved.
//

import UIKit

class ForumViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    var tableView: UITableView!

     override func viewDidLoad() {
        super.viewDidLoad()
    // Do any additional setup after loading the view.
    tableView = UITableView(frame: view.bounds, style: .plain)
    tableView.backgroundColor = UIColor.white
    let cellNib = UINib(nibName: "ForumViewCell", bundle: nil)
    tableView.register(cellNib, forCellReuseIdentifier: "ForumCell")
    view.addSubview(tableView)
    
    var layoutGuide:UILayoutGuide!
    
    if #available(iOS 11.0, *){
    layoutGuide = view.safeAreaLayoutGuide
    } else {
        //fallback on previous versions
        layoutGuide = view.layoutMarginsGuide
    }
    tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
    tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true
    
    tableView.delegate = self
    tableView.dataSource = self
    tableView.reloadData()
}






@IBAction func backTapped(_ sender: Any) {
    
    let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
                   
                   self.view.window?.rootViewController = homeViewController
                   self.view.window?.makeKeyAndVisible()
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 12
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ForumCell", for: indexPath) as! ForumViewController
    
    return cell
}

}

[[我一直得到:“无法将'ForumViewController'类型的返回表达式转换为'UITableViewCell'类型,我已经工作了两天,我的项目下周要上大学。我非常感谢您的帮助。” ] [1]

关于可能需要更改的任何想法?[1]:https//i.stack.imgur.com/Dgxfn.png

科学怪人

您不能将转换UITableViewCellUIViewController替换为:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ForumCell", for: indexPath) as! ForumViewController
    
    return cell
}

有了这个:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    tableView.dequeueReusableCell(withIdentifier: "ForumCell", for: indexPath) // since you're not doing any additional stuff here this would do
}

注意:如果您已经创建了一个xib文件来定义您的UITableViewCell使用布局,则它是向下转换的类。就你而言as! ForumViewCell

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

无法将表达式类型转换为返回类型

Swift Closures:无法将“()”类型的返回表达式转换为“LiveSearchResponse”类型的返回类型?

无法将类型“[Favorite.Type]”的返回表达式转换为返回类型“[Favorite]”

无法将'Bool'类型的返回表达式转换为'Bool'类型的返回值

无法将返回表达式类型转换为带闭包的返回类型?

错误:PickerView“无法将类型为“[String]”的返回表达式转换为返回类型“String?” ”

无法将“FetchedResults<>”类型的返回表达式转换为返回类型“[String]”

无法将类型为Object <String>的返回表达式转换为类型为Object <Character>的返回表达式

无法将表达式的类型'()'转换为'UINavigationController类型?

Swift返回“无法将表达式的类型'NSArray'转换为'NSManagedObject'类型”

无法将“MopubBannerAdView”类型的返回表达式转换为“UIViewController”类型?

无法将类型为'child'的返回表达式转换为类型T

无法将类型[[(key:String,value:Int)]'的返回表达式转换为类型[[String:Int]]

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

无法在 swift 中将“()”类型的返回表达式转换为“String”类型的返回类型

无法将Lambda表达式转换为'int'类型

Lambda表达式中的返回类型错误:无法将Predicate <T>转换为Criteria <T>

将强类型表达式转换为匿名类型返回

无法将类型((AnyPublisher <DemoObject,Never>,APIError)'的返回表达式转换为类型'AnyPublisher <DemoObject,APIError>'

Lambda表达式中的错误返回类型字符串无法转换为Object

Swift 通用结构符合具有关联类型的协议,出现“无法将类型为 'String' 的返回表达式转换为返回类型为 'Key'”错误

无法将 lambda 表达式转换为 int 类型,因为它不是委托类型

无法将lambda表达式转换为类型'bool',因为它不是委托类型

Swift无法将表达式的类型“无效”转换为“字符串!”类型

无法将类型* json.RawMessage的表达式转换为golang中的[] byte类型

无法将表达式类型“ Dictionary”转换为“ StringLiteralConvertible”类型

无法将表达式的类型“ NSDictionary”转换为“ StringLiteralConvertible”类型

无法将lambda表达式转换为类型“ ...”,因为它不是委托类型