首页 > 解决方案 > 使用 SwipeCellKit 自定义 TableViewCell

问题描述

我对 pod SwipeCellKit 的理解有问题。

我有这个简单的设置(用于检索数据的 Firebase)

在我的:

class MyFriendsViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {


    @IBOutlet weak var MyFriendsTableview: UITableView!
    var ref: DatabaseReference!
    var MyFriendslist = [MyFriendsModel]()

我的 cellForRowAt 是这样的:

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

     //   let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends") as! SwipeTableViewCell
      //  cell.delegate = self

当我尝试实现 swipeCellKit 时,如果我输入以下内容,我会从 MyFriendTableViewCell(UIimage,标签引用)中丢失我的引用:

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

有谁知道如何解锁这个?

谢谢!

标签: swiftuitableviewswipe

解决方案


只需更改此:

class MyFriendsTableViewCell: UITableViewCell { ...

对此

class MyFriendsTableViewCell: SwipeTableViewCell { ... 

我们刚刚从SwipeTableViewCell类继承。进行此更改后,您将能够使用您的MyFriendsTableViewCell属性和SwipeTableViewCell属性。

更新代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends", for: indexPath) as! MyFriendsTableViewCell
    cell.delegate = self
    ...
    cell.yourMyFriendsTableCellProperty = ...
    return cell
}

推荐阅读