Utilisation de ce code, je reçois uniquement la balise qui a indexPath.row Comment faire un événement de boutons de celui-ci.
taskplybutton est mon bouton sur lequel je passe indéniables. RoW Pour une section unique, cela aurait fonctionné maintenant comment faire à la section multiple. p>
Y a-t-il une autre façon de savoir quel bouton est cliqué? P>
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) }
4 Réponses :
passe personnalisé un bouton contient une propriété: IndustryPath. Pass indéniables code> à la cellule, lorsque vous cliquez sur le bouton de cette cellule, vous pouvez obtenir le
indexpath actuel p> p> l> l>
indéniables code> sur le bouton de la méthode
celltforat code>. Lorsque vous cliquez sur le bouton, vous pouvez utiliser
bouton.indexpath code> pour obtenir ce que vous voulez. P> li>
ol>
Vous pouvez utiliser une autre méthode comme la balise de passage à la touche de type maintenant sur votre bouton d'action, vous pouvez obtenir comme p> let row = sender.tag%1000
let section = sender/1000
Vous pouvez utiliser la fermeture, s'il vous plaît regarder mes codes:
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.buttonAction = { sender in // do your action // in here, you can get indexPath.row and indexPath.section } cell.TaskPlayButton.addTarget(self, action: #selector(PlayBtnClicked( _:)), for: UIButton.Event.touchUpInside) return cell }
Je viens de vous donner un impact rapide. Cela vous aida simplement sur toute la vue Table de votre projet. Vous pouvez suivre ma réponse ici - https://stackoverflow.com/a/62481414/8135121
Ici c'est pour toi. Vous pouvez l'attraper par position de votre bouton. Je vous l'explique. P>
Faites une extension de vue de table commune sur votre classe. Il suffit d'utiliser ceci. P>
p>
tableView.getCellFrom(sender: sender) { [weak self] (cell, indexPath) in // Just use indexPath in here //This closure return your cell and your button's indexPath also }