Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have multiple section , each cell has a button how to know indexpath.row and indexpath.section if user clicks a button [duplicate]

Using this code i only get tag which has indexpath.row how to make a button event from it.

TaskPlayButton is my button on that i am passing indexpath.row for single section this would have worked by now how to do on multiple section.

Is there any other way of knowing which button is clicked?

    func numberOfSections(in tableView: UITableView) -> Int {
        return mobileBrand.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mobileBrand[section].modelName?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell

        cell.TaskName.text = mobileBrand[indexPath.section].modelName?[indexPath.row]

        cell.TaskPlayButton.tag = indexPath.row

        cell.TaskPlayButton.addTarget(self, action: #selector(PlayBtnClicked( _:)), for: UIButton.Event.touchUpInside)
         return cell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40))
        view.backgroundColor = UIColor.rgb(red: 245, green: 245, blue: 245)

        let lbl = UILabel(frame: CGRect(x: 15, y: 0, width: view.frame.width - 15, height: 40))
        lbl.font = UIFont.systemFont(ofSize: 20)
        lbl.text = mobileBrand[section].brandName
        view.addSubview(lbl)



        return view
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if selectedIndex == indexPath.row && isCollapse == true && selectedSection == indexPath.section{
            return 240

        }else{
            return 60
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if selectedIndex == indexPath.row && selectedSection == indexPath.section{
            if self.isCollapse == false
            {
                self.isCollapse = true
            }else{
                self.isCollapse = false
            }
        }else{
            self.isCollapse = true
        }
        self.selectedIndex = indexPath.row
        self.selectedSection = indexPath.section
        TaskTableView.beginUpdates()
        TaskTableView.endUpdates()
    }

    @objc func PlayBtnClicked(_ sender : UIButton)
    {
        print(sender.tag)
    }
like image 456
jis thottan Avatar asked Jan 18 '26 08:01

jis thottan


1 Answers

you can use another method like pass tag to button like this

cell.button.tag = (indexpath.section * 1000) + indexpath.row

now on your button action you can get like

let row = sender.tag%1000
let section = sender/1000
like image 175
Ruchi Makadia Avatar answered Jan 20 '26 00:01

Ruchi Makadia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!