如何获取选定的单元格索引路径 Swift [英] How to get the selected cell index path Swift

查看:27
本文介绍了如何获取选定的单元格索引路径 Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 VC 中有一个表格视图.单元格内部有一些标签和按钮.我在我的标签中传递了值,现在我正在尝试当我点击一个也在该单元格中的按钮时,它应该增加标签的值.该标签中的值来自之前的 VC.当按下按钮时,我已经为它做了一个委托,当按下按钮时,它应该将 label 的值增加其中存在的第一个价格.我正在尝试获取该单元格索引路径,但没有得到它.我的代码是这样的在我的 cel 课上,我有这个协议,

I have a table view in my VC. Inside the cell there are some lables and buttons. I passed the values in my labels, now i'm trying that when i hit a button that is also in that cell it should increment the value of the label. The value in that label is coming from previous VC. I have made a delegate for it when button is pressed, when button is pressed it should increment the value of label by the first price which is present in it. i'M TRYING to get that cell index path but not geeting it. My code is this, In my cel class i have this protocol,

protocol cartDelegate {
func addTappedInCell(_ cell: CartTableViewCell)
func minusTappedInCell(_ cell: CartTableViewCell)

}

var delegate : cartDelegate?
@IBAction func addBtnTapped(_ sender: Any) {

    delegate?.addTappedInCell(self)
}

@IBAction func minusBtnTapped(_ sender: Any) {

   delegate?.minusTappedInCell(self)
}

在我的视图控制器中,我正在尝试这个,

and in my view controller i'm trying this,

extension CartViewController : cartDelegate{

func addTappedInCell(_ cell: CartTableViewCell) {


    guard let indexPath = cartTableView?.indexPath(for: cell) else { return }

    print(indexPath)

    total += 1
    cell.totalLbl.text = String(total)
    print(cell.priceLbl.text!)
    count = "5"

    let tPrice = cell.priceLbl.text! + count
    print(tPrice)
    cell.priceLbl.text = String(tPrice)
    subTotalLbl.text = cell.priceLbl.text

}

func minusTappedInCell(_ cell: CartTableViewCell) {
    total -= 1
    cell.totalLbl.text = String(total)
    price = Int(cell.priceLbl.text!)! - Int(count)!
    cell.priceLbl.text = String(price)
    subTotalLbl.text = cell.priceLbl.text
}

我没有得到按下按钮的那个单元格的 indexPath.我的屏幕是这样的

I'm not getting the indexPath of that cell which button is pressed. This is how my screen looks,

推荐答案

在自定义单元格类内部声明一个 var

Inside the custom cell class declare a var

   var cellIndex:NSIndexPath?

并在 cellForRow 中设置它

and in cellForRow set it

   cell.cellIndex = indexPath

然后在任何地方访问它

或直接使用

 var index = tableView.indexPath(for:cell)

这篇关于如何获取选定的单元格索引路径 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆