set section and row to UIButton iOS swift

 Problem:

I'm using button in UITableview cell.Tableview had section and rows.I want to get the section and row from button.If i'm using tag then i can only set section or row to button.How to get both section and row from button?

Solution:


private struct AssociatedKeys {

    static var section = "section"

    static var row = "row"

}



extension UIButton {

    

    var section : Int {

        get {

            guard let number = objc_getAssociatedObject(self,   &AssociatedKeys.section) as? Int else {

                return -1

            }

            return number

        }

        set(value) {

            objc_setAssociatedObject(self,&AssociatedKeys.section,Int(value),objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)

        }

    }

    

    var row : Int {

        get {

            guard let number = objc_getAssociatedObject(self, &AssociatedKeys.row) as? Int else {

                return -1

            }

            return number

        }

        set(value) {

            objc_setAssociatedObject(self,&AssociatedKeys.row,Int(value),objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)

        }

    }

    }


You can directly set the section and row to the button


myBtn.section = tableview.section

myBtn.row = tableview.row


Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift