Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Row and Section through Tag in Button Swift

I have this inside cellForRowAtIndexPath

   cell.plusBut.tag = indexPath.row
   cell.plusBut.addTarget(self, action: "plusHit:", forControlEvents: UIControlEvents.TouchUpInside)

and this function outside:

func plusHit(sender: UIButton!){
    buildings[sender.tag].something = somethingElse
}

Is it possible to send the indexPath.row and indexPath.section, or some alternative??

Thanks!

EDIT

I approached it like this:

My Custom Button

class MyButton: UIButton{

    var myRow: Int = 0
    var mySection: Int = 0

}

My Custom Cell

class NewsCell: UITableViewCell{

    @IBOutlet weak var greenLike: MyButton!

In CellForRowAtIndexPath

    cell.greenLike.myRow = indexPath.row

I get an error on this line.

like image 829
Greg Peckory Avatar asked Sep 02 '15 10:09

Greg Peckory


4 Answers

Is it possible to send the indexPath.row and indexPath.section, or some alternative??

If you impose a limit on the number of possible rows in a section, you can combine the two into a single number. For example, if you're willing to say that there will always be fewer than 1000 rows in a given section, you can use:

cell.plusBut.tag = (indexPath.section * 1000) + indexPath.row

and then use mod and division operators to recover:

row = sender.tag % 1000
section = sender.tag / 1000

Another possibility is to check the button's superview (and it's superview, etc.) until you find the cell. Once you have the cell, you can get the index path for that cell from the table.

A third option, perhaps the best one, is to have the button target the cell rather than some other object. The cell can then trigger an action in the view controller or other object using itself as sender.

like image 96
Caleb Avatar answered Oct 15 '22 13:10

Caleb


You can create a subclass of UIButton and create an extra property section in it. And then you can use that class for cell button. You can do the same for row.

Here are few possible ways after subclassing UIButton

cell.plusBut.tag = indexPath.row
cell.plusBut.section = indexPath.section

Or

cell.plusBut.row = indexPath.row
cell.plusBut.section = indexPath.section

Or

cell.plusBut.indexPath = indexPath

Choose whatever suits you.

like image 24
Inder Kumar Rathore Avatar answered Oct 15 '22 14:10

Inder Kumar Rathore


Here's a really easy solution I use:

in CellForRow:

button.tag = indexPath.row
cell.contentView.superview?.tag = indexPath.section

in the function retrieve it like this:

func buttonTapped(_ sender: Any) {
    let button = sender as! UIButton
    let row = button.tag
    let sec = button.superview?.tag

    //retrieve item like self.array[sec][row]
    //do the rest of your stuff here
}
like image 23
David Villegas Avatar answered Oct 15 '22 13:10

David Villegas


Swift 2.x - Extension with Associated Objects

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

usage:

//set
cell.contextMenuBtn.section = indexPath.section
cell.contextMenuBtn.row = indexPath.row 
//read
func showContextMenu (sender:UIButton) {
    let track = dictionarySongs[sender.section][sender.row]
    ...
}
like image 34
iluvatar_GR Avatar answered Oct 15 '22 12:10

iluvatar_GR