确定哪些按钮pressed在斯威夫特一个自定义的ListView细胞 [英] Identifying which button pressed in a custom listView cell in Swift
问题描述
我已经为斯威夫特一个ListView自定义单元格。它有两个按钮 - 一个暂停按钮,一个停止按钮。这个想法是,每个行项目重新presents下载,这样用户可以停止和启动独立每个
I've created a custom cell for a listView in Swift. It has two buttons on it - one a 'pause' button and one a 'stop' button. The idea is that each line item represents a download so the user can stop and start each independently.
不过,我需要为每个按钮的@IBAction。我已经在主要的ViewController创建这些果然,当他们勾搭上了,他们触发适当的。
However, I need to create an @IBAction for each of the buttons. I've created these in the main ViewController and sure enough, when they are hooked up, they trigger appropriate.
位我卡上确定哪一行的按钮的标识已经pressed。我假设与该的cellForRowAtIndexPath会工作的东西。
The bit I'm stuck on is identifying an identifier of which row's button has been pressed. I'm assuming something relating to the cellForRowAtIndexPath would work.
我发现下面的code(这是我从有关文本字段类似的问题找到):
I've found the following code (which I found from a similar question concerning text fields):
@IBAction func startOrPauseDownloadSingleFile(sender: UIButton!) {
let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable)
}
不过,我不断收到错误不能援引convertPoint与类型的参数列表(@lvalue CGPoint,toView:$ T6)'。
However I keep getting the error 'Cannot invoke 'convertPoint' with an argument list of type (@lvalue CGPoint, toView: $T6)'.
谁能帮助?
谢谢,
推荐答案
我把你的code和嵌入它变成一个简单的项目。
I took your code and embedded it into a simple project.
在Interface Builder中,我创建了一个的UITableViewController
现场,并设置其类的ViewController。我添加到它的的UITableViewCell
,设置它的标识符细胞,设置其风格自定义,其类CustomCell。然后,我在单元格中的内容查看添加一个UIButton和非暧昧自动约束布局设置它。
In Interface Builder, I created a UITableViewController
scene and set its class to "ViewController". I added to it a UITableViewCell
, set its identifier to "Cell", set its style to "Custom" and its class to "CustomCell". I then added a UIButton in the cell's contentView and set non ambiguous auto layout constraint to it.
在项目导航,我创建了一个名为的ViewController的新文件,并在其中添加了以下code:
In Project Navigator, I created a new file called "ViewController" and added the following code in it:
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中
到的UIButton
在Interface Builder,跑项目,并能够登录各单元的indexPaths从每一个按钮的触摸。
I finally linked the button's IBOutlet
to the UIButton
in Interface Builder, ran the project and was able to log respective cell's indexPaths from each button touch.
这篇关于确定哪些按钮pressed在斯威夫特一个自定义的ListView细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!