按下按钮时,swift ios将触发表格索引
我的表中有一个按钮,单击该按钮将触发所选的表索引。例如,当我单击所选索引处的按钮时,它将触发下面的代码。多谢各位 代码按下按钮时,swift ios将触发表格索引,ios,swift,selectedindex,Ios,Swift,Selectedindex,我的表中有一个按钮,单击该按钮将触发所选的表索引。例如,当我单击所选索引处的按钮时,它将触发下面的代码。多谢各位 代码 使用以下代码从TableViewCell获取索引: func indexWasPressed(cell: ToDoListTableViewCell) { let indexPath = yourTableView.indexPath(for: cell) let index = indexPath.row } 试试这个:- @objc func butt
使用以下代码从TableViewCell获取索引:
func indexWasPressed(cell: ToDoListTableViewCell) {
let indexPath = yourTableView.indexPath(for: cell)
let index = indexPath.row
}
试试这个:-
@objc func buttonPressed(sender: UIButton) {
// printing selected row
print("index path: \(sender.tag)")
//do you want section try this
var section = Int()
let pointInTable: CGPoint = sender.convert(sender.bounds.origin, to: self.tblView)
let cellIndexPath = self.tblView.indexPathForRow(at: pointInTable)
section(cellIndexPath!)
section = cellIndexPath!.row
print(section)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cell for rows in \(indexPath.section)")
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifire", for: indexPath) as! yourcell
cell.yourbutton.tag = indexpath.row
cell.yourbutton.addTarget(self, action: #selector(buttonpressed(sender:)), for: UIControlEvents.touchUpInside)
}
它正在工作,先生,但是如果我想触发这个unc表视图(uTableView:UITableView,didSelectRowAt indexPath:indexPath){?当按钮被按下时?确定意味着点击单元格按钮时,你必须获得正确的单元格索引?然后你必须实现delegate。你能回答这个问题吗,先生
@objc func buttonPressed(sender: UIButton) {
// printing selected row
print("index path: \(sender.tag)")
//do you want section try this
var section = Int()
let pointInTable: CGPoint = sender.convert(sender.bounds.origin, to: self.tblView)
let cellIndexPath = self.tblView.indexPathForRow(at: pointInTable)
section(cellIndexPath!)
section = cellIndexPath!.row
print(section)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cell for rows in \(indexPath.section)")
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifire", for: indexPath) as! yourcell
cell.yourbutton.tag = indexpath.row
cell.yourbutton.addTarget(self, action: #selector(buttonpressed(sender:)), for: UIControlEvents.touchUpInside)
}