以编程方式从带有动画的 customCell class 的 tableView 中删除一行

Programmatically delete a row from tableView from its customCell class with animation

目标

customTableViewCell

调用的 TableView 中删除一行

我的 CustomTableViewController 中有一个 tableViewdeque 中有一个 customTableViewCell

在那 customTableViewCell class 我有一个 UIButton 动作需要在按下时更改它的 buttonTitle,相应地如果 required.For 是我就删除行本身将该单元格的 indexPath 传递给 customTableViewCell class 中的 globalVariable,我用它来删除通过 CustomTableViewController.

实例访问的行

代码

retrieving 来自 firebase 和 struct 的数据库的代码不需要,仅供参考。

CustomTableViewController 其中嵌入了 tableView:-

class FriendsListController: UIViewController , UITableViewDelegate , UITableViewDataSource,addingFriendDelegate{

  var profileFeed = [profileStruct]() //profileFeed is the an array of type struct, in which i am storing my retrieved database value's
   var customCellHandler = FriendsTableViewCell() 
  override func viewDidLoad() {

    super.viewDidLoad()
    listTableView.delegate = self
    listTableView.dataSource = self
     customCellHandler.delegate = self
    if friendListTable == true && profileFeed.isEmpty == true{

        FIRControllerHandle.retrievingForTheFriendListTableView { (userName, lctn, ID) in

            let temp = profileStruct.init(userName: userName, location: lctn, UID: ID)
            self.profileFeed.insert(temp, atIndex: 0)
            self.listTableView.reloadData()
            self.presentViews()

        }

    }else if friendListTable == false && profileFeed.isEmpty == true{

        print(profileFeed)
        print(profileFeed.isEmpty)

        FIRControllerHandle.retrievingForThePendingFriendRequests({ (userName, location, userId) in

            if self.profileFeed.isEmpty == true{

            let temp = profileStruct.init(userName: userName, location: location, UID: userId)
            self.profileFeed.insert(temp, atIndex: 0)
            self.listTableView.reloadData()
            self.presentViews()
            }
        })
      }
    }

    //Code for storing the data in a struct  is just for reference , trivial w.r.t context of my question

   }


   //addindFriendDelegate methods
  func deleteRowAtTheIndex(index: NSIndexPath){

        listTableView.deleteRowsAtIndexPaths([index], withRowAnimation: .Fade)

     }


//TableView delegate Functions.

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return profileFeed.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = listTableView.dequeueReusableCellWithIdentifier("friendsCell") as! FriendsTableViewCell

    cell.userNameLabel.text = profileFeed[indexPath.row].profileName
    cell.userUID = profileFeed[indexPath.row].profileUserId
    cell.userName = profileFeed[indexPath.row].profileName
    cell.userLocationLabel.text = profileFeed[indexPath.row].profileLocation
    cell.currentUserName = currentuserName_initial
    cell.friendsListBool = friendListTable
    cell.cellIndexPath = indexPath   //Sending the indexPath of that row


    return cell

  }
}

customTableViewCell class 响应按钮操作:-

protocol addingFriendDelegate {
func deleteRowAtTheIndex(index: NSIndexPath)
    }

class FriendsTableViewCell: UITableViewCell {

var userName : String!
var userLocation : String!
var userUID : String!
var FIRControllerHandle : FIRController = FIRController() //I am using firebase as my backend and `FIRController` class handles all the firebase functions.
var delegate: addingFriendDelegate!
var friendsListBool : Bool = true // I am managing two tableViews datasource in one tableView , and `friendsListBool` tells me which node to retrieve from my database.
var currentUserName : String!
var cellIndexPath : NSIndexPath!
var listTableViewController : FriendsListController = FriendsListController() // Instance of viewController in which my tableView is embed


 @IBAction func addFriendsBtnAction(sender: CustomButton) {

    if friendsListBool{
    FIRControllerHandle.sendFriendRequest(userUID, completionBlock: {

           self.addFriendsBtn.setTitle("Sent", forState: .Normal) //Setting the title

           print(self.cellIndexPath.row) //Printing Fine
           self.listTableViewController.listTableView.deleteRowsAtIndexPaths([self.cellIndexPath], withRowAnimation: .Fade)


    })

    } else if !friendsListBool {

        FIRControllerHandle.accepFriendRequest(currentUserName, friendID: userUID, friendName : userName ,completionBlock: {

            self.addFriendsBtn.setTitle("Friends", forState: .Normal)



           })
       }
   }
}

我尝试了什么

self.listTableViewController.listTableView.deleteRowsAtIndexPaths([self.cellIndexPath], withRowAnimation: .Fade)

@IBAction func addFriendsBtnAction(sender: CustomButton) 中调用,其中 listTableViewController 是 viewController 的实例,其中嵌入了我的 tableView,cellIndexPath 我要删除的行的索引路径

错误线

self.listTableViewController.listTableView.deleteRowsAtIndexPaths([self.cellIndexPath], withRowAnimation: .Fade)

错误:- 致命错误:在展开可选值时意外发现 nil

(lldb)

PS:- 我不是特别倾向于 protocol-delegate 方法 this.I 我正在寻找替代方法。

从不 使用默认初始化程序初始化视图控制器,如 FriendsListController()
它将创建一个全新的实例,这不是您在视图层次结构中期望的实例。

协议/委托的合适替代方案是完成闭包。

根据您的最新代码更改以下内容:

  • 删除

    var customCellHandler = FriendsTableViewCell()
    var delegate: addingFriendDelegate!
    
  • 插入cellForRowAtIndexPath

    cell.delegate = self
    
  • 插入deleteRowAtTheIndex

    profileFeed.removeAtIndex(index.row)