Adem Özsayın
Adem Özsayın

Reputation: 247

Custom cell did select not call delegate

I have a custom cell named PendingHistoryCell. When did select i get my stake and by indexpath.row value of the id

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let userStakes = self.userStakes
        let better_bet_status = userStakes[indexPath.row].bet_status
        let deletedOddId = userStakes[indexPath.row]._id

        if better_bet_status == "PENDING" {
            delegate?.deleteBet(oddId: deletedOddId!)
        } else{

        }
    }

and the protocol is PendingHistoryCell

protocol PendingHistoryCellDelegate {
    func deleteBet(oddId: String)
}

And in MyBetsViewContoller i configure the cell

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {

        if(indexPath.row == 0){
            //do some
        } else {
            let cell: PendingHistoryCell = tableView.dequeueReusableCell(withIdentifier: CellId.MyBets.pendingHistoryCell, for: indexPath) as! PendingHistoryCell
            let match = isOnPendingTab ? pendingMatches[indexPath.row-1] : claimedMatches[indexPath.row-1]
            //let matchAllData = self.matchData
            let userStake = (self.matchData?.bets[indexPath.row-1].stakes)!
            //self.fixture = self.matchData?.bets[indexPath.row - 1].fixture

            if(self.matchData?.bets[indexPath.row - 1].fixture != nil){
                self.fixture = self.matchData?.bets[indexPath.row - 1].fixture!
            }

            //cell.configure(match: match, isPending: isOnPendingTab, betCount: self.betCount, matchData: matchAllData!, stakes: userStake, fixture:fixture!  )
            cell.configure(match: match,isPending: isOnPendingTab, betCount: self.betCount, stakes: userStake, fixture: fixture!)
//            func configure( isPending: Bool, betCount: Int,  stakes:[BT_Stake], fixture: BT_Fixture ) {

            return cell
        }
    }

and in my BetsViewController i called

extension MyBetsViewController: PendingHistoryCellDelegate {

    func deleteBet(oddId: String) {
        //do some()
    }

}

but delegates method does not call.

Upvotes: 0

Views: 475

Answers (1)

Iain Coleman
Iain Coleman

Reputation: 406

As others have commented, you need to add the delegate to your tableview cell. To do this, your cell needs the following (in your cell class) :

weak var delegate: PendingHistoryCellDelegate?

To be declared weak (and avoid potential memory leaks), your protocol needs to add : class to its declaration:

protocol PendingHistoryCellDelegate: class {

You can then assign the delegate to your tableview cell in the cellForRow method:

cell.delegate = self

Let me know how you get on!

Upvotes: 2

Related Questions