UIScrollView中的最后一个按钮添加目标没有效果

时间:2017-03-15 03:25:01

标签: ios uiscrollview swift3 uibutton

我有一个类别添加列表,我在滚动视图中为每个类别创建了按钮 最后一个按钮是所有产品的ALL按钮。但是有些我没有采取任何行动。 这是我的代码 首先,我创建一个函数来创建要添加到视图的按钮

func catButtonView(buttonSize:CGSize) -> UIView {
    let buttonView = UIView()
    buttonView.backgroundColor = UIColor(colorLiteralRed: 1, green: 1, blue: 1, alpha: 0)
    buttonView.frame.origin = CGPoint(x: 0,y: 0)
    let padding = CGSize(width:0, height:0)
    buttonView.frame.size.width = (buttonSize.width + padding.width) * CGFloat(categories.count)
    buttonView.frame.size.height = (buttonSize.height +  2.0 * padding.height )
    //add buttons to the view
    var buttonPosition = CGPoint(x:padding.width * 0.5, y:padding.height)
    let buttonIncrement = buttonSize.width + padding.width
    let hueIncrement = 1.0 / CGFloat(categories.count)
    var newHue = hueIncrement
    for i in 0...(categories.count)  {

        let button = UIButton.init(type: .custom) as UIButton
        button.frame.size = buttonSize
        button.frame.origin = buttonPosition
        button.setBackgroundImage(R.image.button(), for: .normal)
        button.setBackgroundImage(R.image.button_selected(), for: .selected)

        if(i==categories.count) {
            button.setTitle("ALL", for: .normal)
        } else {
            button.setTitle(categories[i].name, for: .normal)
        }
        button.setTitleColor(UIColor.white, for: .normal)
        buttonPosition.x = buttonPosition.x + buttonIncrement
        newHue = newHue + hueIncrement
        button.tag = i+1
        button.addTarget(self, action: #selector(catButtonPressed(sender:)), for: .touchUpInside)
        buttonView.addSubview(button)


    }
    return buttonView
}

这是动作功能

func catButtonPressed(sender:UIButton){
    print(sender.tag)
    if(self.selectedCat != sender.tag-1) {
        let lastBtn = catScrollView.viewWithTag(selectedCat+1) as? UIButton
        lastBtn?.setBackgroundImage(R.image.button(), for: .normal)
        self.selectedCat = sender.tag-1
        self.itemsCollection.reloadData()
        sender.setBackgroundImage(R.image.button_selected(), for: .normal)
    }
}

然后添加到滚动视图

let catScrollingView = catButtonView(buttonSize: CGSize(width: 200.0, height:50.0))
    catScrollView.contentSize = catScrollingView.frame.size
    catScrollView.subviews.forEach({ $0.removeFromSuperview() })
    catScrollView.addSubview(catScrollingView)
    catScrollView.showsHorizontalScrollIndicator = true
    catScrollView.indicatorStyle = .default

    self.itemsCollection.reloadData()

任何人都知道我错过了什么?

1 个答案:

答案 0 :(得分:0)

这是因为if statement函数中的catButtonPressed对于最后一个按钮的标记是错误的,这使得内部代码永远不会被执行。

假设您有 5 类别,那么您就是categories.count = 5。因此,您的self.selectedCat范围从0到4 。在for i in 0...(categories.count)最后一个按钮的标记为button.tag = 5

现在在您的catButtonPressed功能中。如果最后一个按钮是最后一个类别,那么您的self.selectedCat 4 ,而sender.tag 5

func catButtonPressed(sender:UIButton){
    print(sender.tag)
    if(4 != 5-1) { 
         // Any code in here will never be executed because 4 != 4 is false
    }
}