GOAL
从 customTableViewCell
调用的TableView中删除一行
我的 CustomTableViewController
中有 tableView
, deque 是 customTableViewCell
.
在 customTableViewCell
类中我有一个 UIButton
Action需要在按下时更改其buttonTitle,并相应地删除行本身(如果需要) . 为此我将该单元格的indexPath传递给 customTableViewCell
类中的globalVariable,我用它来删除我的行通过 CustomTableViewController
的实例访问 .
CODE
来自firebase的数据库 retrieving
的代码和 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
类响应按钮动作: -
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)
})
}
}
}
WHAT HAVE I TRIED
self.listTableViewController.listTableView.deleteRowsAtIndexPaths([self.cellIndexPath], withRowAnimation: .Fade)
在 @IBAction func addFriendsBtnAction(sender: CustomButton)
中调用,其中 listTableViewController
是我的tableView嵌入的viewController的实例,以及我要删除的行的 cellIndexPath
indexPath
LINE OF ERROR
self.listTableViewController.listTableView.deleteRowsAtIndexPaths([self.cellIndexPath], withRowAnimation: .Fade)
错误: - 致命错误:在展开Optional值时意外发现nil
(LLDB)
PS: - 我并不特别倾向于 protocol-delegate
方法 . 我正在寻找替代方案 .
1 回答
Never 使用默认初始化程序(如
FriendsListController()
)初始化视图控制器 .它将创建一个全新的实例,它不是您在视图层次结构中所期望的实例 .
协议/委托的合适替代方案是完成关闭 .
根据您的最新代码更改以下内容:
cellForRowAtIndexPath
deleteRowAtTheIndex