Objective c 识别Swift中自定义listView单元格中按下的按钮
我已经在Swift中为listView创建了一个自定义单元格。它有两个按钮,一个是“暂停”按钮,另一个是“停止”按钮。其思想是,每个行项目代表一个下载,因此用户可以独立地停止和启动每个行项目 但是,我需要为每个按钮创建一个@iAction。我已经在主ViewController中创建了这些,当它们连接起来时,它们会触发相应的事件 我一直坚持的一点是识别按下了哪一行按钮的标识符。我假设与cellForRowAtIndexPath相关的东西会起作用 我找到了以下代码(我从一个关于文本字段的类似问题中找到): 但是,我不断收到错误“无法使用类型为(@lvalue CGPoint,toView:$T6)的参数列表调用'convertPoint' 有人能帮忙吗Objective c 识别Swift中自定义listView单元格中按下的按钮,objective-c,listview,swift,uibutton,Objective C,Listview,Swift,Uibutton,我已经在Swift中为listView创建了一个自定义单元格。它有两个按钮,一个是“暂停”按钮,另一个是“停止”按钮。其思想是,每个行项目代表一个下载,因此用户可以独立地停止和启动每个行项目 但是,我需要为每个按钮创建一个@iAction。我已经在主ViewController中创建了这些,当它们连接起来时,它们会触发相应的事件 我一直坚持的一点是识别按下了哪一行按钮的标识符。我假设与cellForRowAtIndexPath相关的东西会起作用 我找到了以下代码(我从一个关于文本字段的类似问题中
谢谢,我将您的代码嵌入到一个简单的项目中 在Interface Builder中,我创建了一个
UITableViewController
场景,并将其类设置为“ViewController”。我添加了一个UITableViewCell
,将其标识符设置为“Cell”,将其样式设置为“Custom”,将其类设置为“CustomCell”。然后,我在单元格的contentView中添加了一个UIButton,并为其设置了无歧义的自动布局约束
在Project Navigator中,我创建了一个名为“ViewController”的新文件,并在其中添加了以下代码:
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet weak var button: UIButton!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
class ViewController: UITableViewController {
func buttonPressed(sender: AnyObject) {
let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable)
println(cellIndexPath)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomCell
cell.selectionStyle = .None
cell.button.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
}
最后,我将按钮的
IBOutlet
链接到Interface Builder中的ui按钮
,运行了该项目,并能够记录每个按钮触摸时相应单元格的索引路径。我遵循了您的方案,一切正常。但是我尝试在一个单元格中添加更多的UI项,结果还是成功了,但是当我滚动一个单元格时,当它从视图中退出,并且返回时,它似乎不记得约束,并且所有按钮都位于单元格的0,0坐标处。如何解决这个问题?
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet weak var button: UIButton!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
class ViewController: UITableViewController {
func buttonPressed(sender: AnyObject) {
let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable)
println(cellIndexPath)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomCell
cell.selectionStyle = .None
cell.button.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
}