如何从单个tableViewcell获取多个按钮? [英] How to get multiple buttons from a single tableViewcell?

查看:82
本文介绍了如何从单个tableViewcell获取多个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在具有4个按钮(选项)的 tableView 中进行测验,我在诸如201,202,203,204的故事板上标记了它们,并在 tableView 中成功地将它们全部获得方法.但是在将目标添加到按钮之后,我无法在 buttonClicked 方法中获得特定的按钮.

  func numberOfSectionsInTableView(tableView:UITableView)->诠释{return 1}func tableView(tableView:UITableView,numberOfRowsInSection部分:Int)->诠释{returnquestions.count}func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell {让单元格= tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath:indexPath)作为UITableViewCell(cell.viewWithTag(100)as!UILabel).text ="Q:" +(questions [indexPath.row] .objectForKey("MocQuestion")!as?String)!(cell.viewWithTag(100)as!UILabel).font = themeFont(cell.viewWithTag(101)as!UILabel).text = questions [indexPath.row] .objectForKey("Op1")!作为?细绳(cell.viewWithTag(102)as!UILabel).text = questions [indexPath.row] .objectForKey("Op2")!作为?细绳(cell.viewWithTag(103)as!UILabel).text = questions [indexPath.row] .objectForKey("Op3")!作为?细绳(cell.viewWithTag(104)as!UILabel).text = questions [indexPath.row] .objectForKey("Op4")!作为?细绳让btn1 =(cell.viewWithTag(201)as!UIButton)让btn2 =(cell.viewWithTag(202)as!UIButton)让btn3 =(cell.viewWithTag(203)as!UIButton)让btn4 =(cell.viewWithTag(204)as!UIButton)//btn1.tag = indexPath.row * 100 + 0//btn1.tag = indexPath.row * 100 + 1//btn1.tag = indexPath.row * 100 + 2//btn1.tag = indexPath.row * 100 + 3btn1.addTarget(self,action:#selector(Quiz.buttonClicked(_ :)),forControlEvents:UIControlEvents.TouchUpInside)btn2.addTarget(self,action:#selector(Quiz.buttonClicked(_ :)),forControlEvents:UIControlEvents.TouchUpInside)btn3.addTarget(self,action:#selector(Quiz.buttonClicked(_ :)),forControlEvents:UIControlEvents.TouchUpInside)btn4.addTarget(self,action:#selector(Quiz.buttonClicked(_ :)),forControlEvents:UIControlEvents.TouchUpInside)返回单元}func buttonClicked(sender:UIButton){让标签= sender.tag打印(标签)} 

解决方案

如果您希望 indexPath 访问问题 Array ,则可以尝试这样.

  func buttonClicked(sender:UIButton){让中心= sender.center让点= sender.superview!.convertPoint(center,toView:self.tableView)让indexPath = self.tableView.indexPathForRowAtPoint(point)//现在您已经有了按钮检查的标签如果(sender.tag == 201){打印(选项A")}否则(sender.tag == 202){打印(选项B")}否则(sender.tag == 203){打印(选项C")}别的 {打印(选项D")}打印(question [indexPath.row])} 

I am making a quiz in a tableView that has 4 Buttons (options), I tagged them on a story board like 201,202,203,204 and got all of them successfully in tableView methods. But after adding targets to buttons, I am not able to get particular buttons in buttonClicked method.

func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return questions.count }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    (cell.viewWithTag(100) as! UILabel).text = "Q : " + (questions[indexPath.row].objectForKey("MocQuestion")! as? String)!
    (cell.viewWithTag(100) as! UILabel).font = themeFont
    (cell.viewWithTag(101) as! UILabel).text = questions[indexPath.row].objectForKey("Op1")! as? String
    (cell.viewWithTag(102) as! UILabel).text = questions[indexPath.row].objectForKey("Op2")! as? String
    (cell.viewWithTag(103) as! UILabel).text = questions[indexPath.row].objectForKey("Op3")! as? String
    (cell.viewWithTag(104) as! UILabel).text = questions[indexPath.row].objectForKey("Op4")! as? String

    let btn1 = (cell.viewWithTag(201) as! UIButton)
    let btn2 = (cell.viewWithTag(202) as! UIButton)
    let btn3 = (cell.viewWithTag(203) as! UIButton)
    let btn4 = (cell.viewWithTag(204) as! UIButton)


//        btn1.tag = indexPath.row * 100 + 0
//        btn1.tag = indexPath.row * 100 + 1
//        btn1.tag = indexPath.row * 100 + 2
//        btn1.tag = indexPath.row * 100 + 3


    btn1.addTarget(self, action: #selector(Quiz.buttonClicked(_:)),forControlEvents: UIControlEvents.TouchUpInside)
    btn2.addTarget(self, action: #selector(Quiz.buttonClicked(_:)),forControlEvents: UIControlEvents.TouchUpInside)
    btn3.addTarget(self, action: #selector(Quiz.buttonClicked(_:)),forControlEvents: UIControlEvents.TouchUpInside)
    btn4.addTarget(self, action: #selector(Quiz.buttonClicked(_:)),forControlEvents: UIControlEvents.TouchUpInside)

    return cell
}

func buttonClicked(sender:UIButton)
{
    let tag = sender.tag
    print(tag)
}

解决方案

If you want the indexPath to access the questions Array then you can try like this.

func buttonClicked(sender:UIButton) {
    let center = sender.center
    let point = sender.superview!.convertPoint(center, toView:self.tableView)
    let indexPath = self.tableView.indexPathForRowAtPoint(point)
    //Now you have tag of button check for that
    if (sender.tag == 201) {
        print("Option A")
    }
    else if (sender.tag == 202) {
        print("Option B")
    }
    else if (sender.tag == 203) {
        print("Option C")
    }
    else {
        print("Option D")
    }
    print(question[indexPath.row])
}

这篇关于如何从单个tableViewcell获取多个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆