【问题标题】:How to get multiple buttons from a single tableViewcell?如何从单个 uitableViewcell 获取多个按钮?
【发布时间】:2017-01-10 08:57:24
【问题描述】:

我在有 4 个按钮(选项)的 tableView 中进行测验,我将它们标记在故事板上,例如 201,202,203,204,并在 tableView 方法中成功获得所有这些按钮。但是在向按钮添加目标后,我无法在buttonClicked 方法中获取特定按钮。

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)
}

【问题讨论】:

  • 那么问题是什么?你的标签应该打印完美。您应该无法识别正确的行!

标签: ios swift uitableview button tags


【解决方案1】:

如果您希望indexPath 访问问题Array,那么您可以这样尝试。

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])
}

【讨论】:

  • 但选项 A 是针对哪个问题?另外,我还需要其他 3 个按钮来进行更改。但针对特定问题。
  • 对于(questions[indexPath.row].objectForKey("MocQuestion")这个问题我想!!
  • 仔细阅读我的答案,它会给你indexPath,从中你可以得到点击哪个按钮的问题。也无需更改cellForRowAtIndexPath中的标签。
【解决方案2】:

swift 3你可以试试下面这样

  1. 我的表格单元格类

    class Custom_Cell: UITableViewCell {    
    
      @IBOutlet weak var ButtonA: UIButton!
      @IBOutlet weak var ButtonB: UIButton!
      @IBOutlet weak var ButtonC: UIButton!
    
     }
    
  2. 在表格视图中设置标签

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Custom_Cell;
    
    
    
       cell.ButtonA.tag = indexPath.row;
       cell.ButtonB.tag = indexPath.row;
       cell.ButtonA.tag = indexPath.row;
    
        //Add Action Methods to UIButtons
         cell.ButtonA.addTarget(self, action: #selector(ButtonAAction), for: .touchUpInside)
        cell.ButtonB.addTarget(self, action: #selector(ButtonBAction), for: .touchUpInside)
        cell.ButtonA.addTarget(self, action: #selector(ButtonCAction), for: .touchUpInside)
    
       return cell;
    }
    
  3. 按钮动作看起来像..

    func ButtonAAction(_ sender: Any)  {
       //Get Button cell position.
       let ButtonPosition = (sender as AnyObject).convert(CGPoint.zero, to: tableView)
       let indexPath = tableView.indexPathForRow(at: ButtonPosition)
       if indexPath != nil {
    
    
           print("Cell indexPath: \(indexPath?.row)")
        }
      }
    

【讨论】:

    【解决方案3】:

    为了获得每个问题单独的按钮click event,您可以将唯一 ID 作为后缀或前缀传递,例如每个问题的 20101 或 01201 作为按钮的标签而不是硬编码。然后拿到tag,先提取ques id,现在按照ques进行检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2019-03-10
      • 2012-03-27
      • 2017-06-26
      • 1970-01-01
      相关资源
      最近更新 更多