简体   繁体   English

如何在UITableViewController中引用自定义UIView的子视图按钮

[英]How to Refer to a Subview Button of Custom UIView in UITableViewController

I'm facing problems with UITableViewCell being reused, and I am receiving suggestions to make the action of a UIButton occur within the UITableViewController. 我在重用UITableViewCell时遇到问题,并且我收到一些建议,建议在UITableViewController中进行UIButton的操作。 The problem is that because the UIButton is instantiated within the custom UIView subclass, I cannot find a way to refer to it. 问题在于,因为UIButton是在自定义UIView子类中实例化的,所以我找不到引用它的方法。

The code is: 代码是:

class StarButton: UIView {

var buttonRow : Int = 0
var buttonSelected : Bool = false

override init (frame : CGRect)
{
    super.init(frame : frame)
    initStar()
}

required init?(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)
    initStar()
}

func initStar(){

    let filledStarImage = UIImage(named: "filledStar")
    let emptyStarImage = UIImage(named: "emptyStar")

    let button = UIButton(frame: CGRect(x: 2, y: 2, width: 33, height: 33))

    button.userInteractionEnabled = true
    button.addTarget(self, action: #selector(StarButton.fillingStar(_:)), forControlEvents: UIControlEvents.TouchUpInside)

    button.setImage(emptyStarImage, forState: .Normal)
    button.setImage(filledStarImage, forState: .Selected)

    if buttonSelected == true{
        button.selected = true
    }

    addSubview(button)
}

//Could have various errors
func fillingStar(sender: UIButton){
    if (sender.selected) == false{
        FavoritesManager.favoritesList.setObject(ChemQuizCollection.wordListObject.quizList[buttonRow], forKey: "\(buttonRow)")
        sender.selected = !sender.selected
        FavoritesManager.favoritesList.synchronize()
    } else{
        FavoritesManager.favoritesList.removeObjectForKey("\(buttonRow)")
        sender.selected = !sender.selected
        FavoritesManager.favoritesList.synchronize()
    }

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    for view in subviews
    {
        view.touchesBegan(touches, withEvent: event)
    }
}

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool
{
    return true
}

} }

This is the custom UIView subclass. 这是自定义UIView子类。 I want to refer to the "button" in func initStar(). 我想引用func initStar()中的“按钮”。 Using for subview in instantiatedStarButton.subviews{ subview.selected = true } throws an error saying UIView doesn't have member "selected". 在InstantializedStarButton.subviews {subview.selected = true}中用于子视图会引发错误,指出UIView没有“选择”成员。 Is there a better way? 有没有更好的办法?

Thank you all in advance. 谢谢大家。

The problem is with your code 问题出在你的代码上

StarButton.subviews{ subview.selected = true }

.subviews give UIView not UIButton : UIView has no .selected property .subviewsUIView UIButtonUIView没有.selected属性

What should be done is following code or something similar. 应该执行以下代码或类似的操作。 You have to cast tour UIView to UIButton 您必须将游览UIView投射到UIButton

let filtured  =  instantiatedStarButton.subviews.filter({
            if let button = $0 as? UIButton{
                return button.selected
            }
            return false
        })

buttonSelected is set to false while you are only changing the state of the button if its true. 当您仅更改按钮的状态为true时,buttonSelected设置为false。 That's why you're getting the error. 这就是为什么您得到错误。

You can either change the buttonSelected to true, or even better assign a tag to the button and compare that tag with the 'sender' in fillingStar. 您可以将buttonSelected更改为true,或者甚至可以为按钮分配标签,并将该标签与fillingStar中的“发送者”进行比较。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM