简体   繁体   English

如何检测Swift 2中单击的表格单元格

[英]How to detect which table cell is clicked in swift 2

I am developing app which users will choose one of the two pictures in one cell. 我正在开发应用程序,用户可以在一个单元格中选择两张图片之一。 My prototype cell looks like : 我的原型单元格看起来像:

原型细胞

How can I detect when the user presses the vote button which cell is selected ? 我如何检测用户何时按下投票按钮选择了哪个单元格?

My tableView Code : 我的tableView代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cellIdentifier = "NewTableViewCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! NewTableViewCell

        //For left image
        if let url:NSURL? = NSURL(string: self.polls[indexPath.row].poll_photo1 ){
            cell.leftImage.sd_setImageWithURL(url)
        }
        //For right image
        if let url:NSURL? = NSURL(string: self.polls[indexPath.row].poll_photo2 ){
            cell.rightImage.sd_setImageWithURL(url)

        }

        //For user picture
        if let url:NSURL? = NSURL(string: self.polls[indexPath.row].users[0].user_photo ){
            cell.userPicture.sd_setImageWithURL(url)
        }

        // gets username and text
        cell.userName.text=self.polls[indexPath.row].users[0].user_name
        cell.description.text = self.polls[indexPath.row].poll_textfield

        return cell
    }

My web API: 我的Web API:

在此处输入图片说明

I am assuming that your custom table view cell NewTableViewCell is having an outlet for your vote button. 我假设您的自定义表格视图单元格NewTableViewCell正在为您的投票按钮提供出口。
Just tag your voteButton with indexPath.row and fetch the tag in its target function as shown below. 只需使用indexPath.row标记您的voteButton indexPath.row并在其目标函数中获取标记,如下所示。 You will get to know which cell's voteButton was tapped when you press your Vote Button 您将了解哪些细胞的voteButton被窃听当您按下按钮投票

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("NewTableViewCell") as! NewTableViewCell

    //Tagging with indexPath.row
    cell.voteLeftButton.tag = indexPath.row
    cell.voteRightButton.tag = indexPath.row


    //This is the latest Swift 2.2 syntax for selector. If you are using the older version of Swift, Kindly check the selector syntax and make changes accordingly
    cell.voteLeftButton.addTarget(self, action: #selector(voteLeftButtonPressed), forControlEvents: .TouchUpInside)
    cell.voteRightButton.addTarget(self, action: #selector(voteRightButtonPressed), forControlEvents: .TouchUpInside)
    return cell

}

func voteLeftButtonPressed(sender:UIButton){

    print("Left Button Table cell clicked is \(sender.tag)")

}

func voteRightButtonPressed(sender:UIButton){

    print("Right Button Table cell clicked is \(sender.tag)")

}

Add target/action to your button in cell configure method like this: 在单元格配置方法中将目标/操作添加到您的按钮,如下所示:

 let tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourController.tapAction(_:)))

Then implement tapAction method 然后实现tapAction方法

func tapAction(sender : UITapGestureRecognizer)
  {

    if sender.state != UIGestureRecognizerState.Ended
    {
        return

    }
    let btn = sender.view as! UIButton

    let pointTo : CGPoint  = CGRectOffset(btn.bounds, btn.frame.size.width/2, btn.frame.size.height/2).origin;
    let buttonPosition : CGPoint  = btn.convertPoint(pointTo, toView: self.tableView)

    let indexPath = self.tableView.indexPathForRowAtPoint(buttonPosition)


    ...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测 swift 中触摸或点击的 tableView 单元格 - How to detect tableView cell touched or clicked in swift 检测在“单元格”上单击了哪个“视图” - Detect which `View` was clicked on a `Cell` 如何检测单元格是否被选中但未在UITableViewController中单击 - How to detect that a cell is selected but not clicked in a UITableViewController 如何检测用户点击SKStoreReviewController的按钮? - How to detect which button user clicked on SKStoreReviewController? 在Swift中单击Table View的单元格时如何调用(显示)另一个View Controller - How to call (show) another View Controller when clicked on the cell of Table View in Swift 在 Swift 中单击表视图单元格时如何显示新的视图控制器 - How to show new View Controller when a Table View Cell is clicked in Swift 使用自定义表格视图单元检测用户所在的单元格 - Detect Which Cell a User is in With Custom Table View Cell 如何判断从另一个View Controller单击哪个表格单元格? IOS 7 - How to tell which table cell was clicked from another View Controller? iOS 7 如何检测 UITableView 中的单元格选择 - Swift - How to detect Cell selection in UITableView - Swift 如何知道在Swift中单击哪个UIView - How to know which UIView is clicked In Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM