代码之家  ›  专栏  ›  技术社区  ›  Chen Li Yong

无法将“UITableViewCell”类型的值强制转换为“SwipeCellKit.SwipeTableViewCell”

  •  1
  • Chen Li Yong  · 技术社区  · 6 年前

    SwipeCellKit 在我的项目中,这个pod添加了一个名为SwipeTableViewCell的类。到目前为止,我所做的是:

    • 创建一个 UIViewController
    • 进口 SwipeCellKit 到Swift文件。
    • 将视图控制器指定给类。
    • 将委托和数据源从表视图连接到视图控制器。
    • 将原型单元添加到表视图中,并将其命名为“单元”,因为它的可重用标识符。
    • 将单元格类更改为SwipeTableViewCell。
    • 将表视图委托添加到视图控制器。
    • 添加 numberOfRows cellForRow

    cellForRow :

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

    let cell = ... 他说:

    无法将“UITableViewCell”类型的值强制转换为“SwipeCellKit.SwipeTableViewCell”

    2 回复  |  直到 6 年前
        1
  •  5
  •   PaianuVlad23    5 年前

    是什么为我解决了这个问题:

    主故事板

    • 类设置为 可切换可视单元
    • 模块设置为 SwipeCellKit .
        2
  •  2
  •   Reinier Melian    6 年前

    您需要创建自己的Cell类作为 SwipeTableViewCell 然后您需要实现 SwipeTableViewCellDelegate ViewController中的协议

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SwipeTableViewCell
    

    您应该使用类名,它是 子类

    实例

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MySwipeableCell
    cell.delegate = self
    
        3
  •  1
  •   Omar Masri    6 年前

    我创建了以下示例

    import UIKit
    import SwipeCellKit
    
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    
        @IBOutlet weak var tableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 4
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SwipeTableViewCell
            return cell
        }
    
    }
    
    
    class SwipeTableViewCellSubClass: SwipeTableViewCell {
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
    }
    

    将视图的类设置为SwipeTableViewCellSubClass。

    这对你来说应该很合适。祝你好运

        4
  •  0
  •   Marcosc    4 年前

    要修复而不创建自定义类,只需单击原型单元:

    Select the prototype cell

    Add the class and module

        5
  •  0
  •   usama gujar    4 年前

    之所以出现此错误,是因为您在viewDidLoad()ftn()中设置了单元格值。

        6
  •  0
  •   Quick learner    3 年前

    enter image description here

    选择原型单元并将类更改为 SwipeTableViewCell SwipeCellKit