以编程方式生成的按钮不执行操作

时间:2016-09-13 10:56:18

标签: ios swift uibutton

我试图在UITableViewCell中动态添加一些按钮。我创建了方法" tappedButton"当任何这些按钮被点击但它不能正常工作时应该调用它。我需要按3或4次然后几乎没有动作发生。任何人都建议我更好的方法来做到这一点。

    func tappedButton(sender: UIButton!) {
            print("tapped button---->"+(sender.titleLabel?.text)!)
            self.performSegueWithIdentifier("web", sender: self)
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! MagazineTableViewCell
                 // headings is global variable , an array of string
                 for td in headings {
                    let button:UIButton = UIButton(frame: CGRectMake(0, t, 500, 26))
                    t+=36
                    button.setTitle(td,forState: UIControlState.Normal)
                    let black=hexStringToUIColor("#000000")
                    let white=hexStringToUIColor("#ffffff")
                    button.backgroundColor = black
                    button.setTitleColor(white, forState: UIControlState.Normal)
                    button.addTarget(self, action: #selector(MainTableViewController.tappedButton(_:)), forControlEvents: UIControlEvents.AllTouchEvents)
                    // searchView is a black UIView in which I dynamically add buttons
                    cell.searchView.addSubview(button)
                  }

         return cell
    }

4 个答案:

答案 0 :(得分:0)

您应该在单元格的子类中添加Button。然后在你的cellforRowAtIndexParh中添加addTarget。请试一试,让我知道。 +使用.touchDown更改addTarget而不是.AllTouchEvents

答案 1 :(得分:0)

您最好将UIButton添加到原型单元格中,然后添加

yourButton.hidden = true 

如果你想隐藏它。请注意,您应该添加

yourButton.hidden = false 

let cell = ..

使细胞重复使用时能正常工作。

答案 2 :(得分:0)

让我们尝试更新下面的cellForRowAtIndexPath方法。

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

        //remove existing buttons
        for subView in cell.searchView.subviews{

            subView.removeFromSuperview()
        }

        // headings is global variable , an array of string
        for td in headings {
            let button:UIButton = UIButton(frame: CGRectMake(0, t, 500, 26))
            t+=36
            button.setTitle(td,forState: UIControlState.Normal)
            let black=hexStringToUIColor("#000000")
            let white=hexStringToUIColor("#ffffff")
            button.backgroundColor = black
            button.setTitleColor(white, forState: UIControlState.Normal)
            button.addTarget(self, action: #selector(MainTableViewController.tappedButton(_:)), forControlEvents: UIControlEvents.touchUpInside)
            // searchView is a black UIView in which I dynamically add buttons
            cell.searchView.isUserInteractionEnabled = true
            cell.searchView.addSubview(button)
        }

        return cell
    }

您正尝试在cellForRowAtIndexPath方法中将按钮添加到单元格,只要cellForRowAtIndexPath调用,按钮就会重叠。所以首先你应该从searchView删除现有按钮,并在for循环中添加动态(标题)按钮。

答案 3 :(得分:0)

我已将您的方案复制到基本项目中:

class ViewController: UIViewController {
  @IBOutlet weak var tableView: UITableView!

  let numberOfButtons = 3
  var currentButtonYPosition: CGFloat = 0

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
  }

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

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

    for index in 0..<numberOfButtons {
      let button = UIButton()
      button.frame = CGRectMake(0, currentButtonYPosition, tableView.frame.width, 30)
      button.backgroundColor = UIColor.redColor()
      button.setTitle(String(index), forState: .Normal)
      button.addTarget(self, action: #selector(buttonWasTapped(_:)), forControlEvents: .TouchUpInside)

      cell.innerView.addSubview(button)

      currentButtonYPosition += 40
    }

    return cell
  }

  func buttonWasTapped(button: UIButton) {
    print("button \(button.currentTitle!) was tapped")
  }
}

结果