gpt4 book ai didi

swift - 我有多个部分,每个单元格都有一个按钮,如果用户单击一个按钮,如何知道 indexpath.row 和 indexpath.section

转载 作者:行者123 更新时间:2023-12-05 03:52:02 28 4
gpt4 key购买 nike

<分区>

使用此代码,我只获得具有 indexpath.row 如何从中创建按钮事件的标签。

TaskPlayButton 是我正在传递 indexpath.row 的按钮对于单个部分,这在现在如何在多个部分上工作。

有没有其他方法知道点击了哪个按钮?

    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)
}

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com