目标
从customTableViewCell
我的tableView
中有一个CustomTableViewController
,其中 deque 是customTableViewCell
。
在customTableViewCell
类中,我有一个UIButton
动作,需要在按下时更改其buttonTitle,相应地删除行本身(如果需要)。为此我将该单元格的indexPath传递给global customTableViewCell
类中的globalVariable,用于删除通过CustomTableViewController
实例访问的行。
CODE
来自firebase和retrieving
的{{1}}数据库的代码是不必要的,仅供参考。
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)
})
}
}
}
在self.listTableViewController.listTableView.deleteRowsAtIndexPaths([self.cellIndexPath], withRowAnimation: .Fade)
中调用,其中@IBAction func addFriendsBtnAction(sender: CustomButton)
是我的tableView嵌入的viewController的实例,以及我要删除的行的listTableViewController
indexPath
LINE OF ERROR
cellIndexPath
错误: - 致命错误:在解包可选值时意外发现nil
(LLDB)
PS: - 我并不特别倾向于self.listTableViewController.listTableView.deleteRowsAtIndexPaths([self.cellIndexPath], withRowAnimation: .Fade)
方法。我正在寻找替代方案。
答案 0 :(得分:2)
从不使用FriendsListController()
等默认初始值设定项初始化视图控制器
它将创建一个全新的实例,而不是您在视图层次结构中所期望的实例。
协议/委托的合适替代方案是完成关闭。
根据您的最新代码更改以下内容:
删除
var customCellHandler = FriendsTableViewCell()
var delegate: addingFriendDelegate!
插入cellForRowAtIndexPath
cell.delegate = self
插入deleteRowAtTheIndex
profileFeed.removeAtIndex(index.row)