我正在使用Swift创建一个应用程序.我有一个UITableView我填充数据库中的一些数据.当用户点击一个单元格时,我想触发一个动作.
我做了什么 :
var array: [String] = ["example"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell(style :UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel.text = array[indexPath.row]
cell.tag = indexPath.row
cell.targetForAction("getAction:", withSender: self)
return cell
}
func getAction(sender:UITableViewCell)->Void {
if(sender.tag == 0) {
println("it worked")
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试从另一个帖子调整解决方案,但我做错了.先感谢您
我正在使用swift创建一个应用程序.在我的一个ViewController中,我有一个以编程方式创建的GMSMapView.我希望用户能够在单击地图时触发操作.
我做了什么 :
import UIKit
class MapViewController: UIViewController, GMSMapViewDelegate {
let mapView = GMSMapView()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.settings.scrollGestures = false
mapView.frame = CGRectMake(0, 65, 375, 555)
view.addSubview(mapView)
var tap = UITapGestureRecognizer(target: self, action: "tap:")
mapView.addGestureRecognizer(tap)
}
func tap(recogniser:UITapGestureRecognizer)->Void{
println("it works")
}
}
Run Code Online (Sandbox Code Playgroud)
我试图覆盖touchesBegan,没有工作.我试图插入mapView.userInteractionEnabled = true,没有工作......
任何的想法?