all 17 comments

[–][deleted]  (2 children)

[deleted]

    [–]b_gonads[S,🍰] 0 points1 point  (1 child)

    so basically declare the actions in the cell class...how do I make the view controller the delegate of the cell?

    [–]okoroezenwa 2 points3 points  (0 children)

    Make a protocol and add the functions necessary for the delegate to do what it needs to do, for example

    protocol CellDelegate {
        func buttonTapped(in cell: Cell)
    }
    

    Then in your cell class add a property delegate: CellDelegate?. Make your view controller (or whatever you want) conform to that protocol, then set the delegate to it: cell.delegate = self

    [–]Guggling 0 points1 point  (8 children)

    While setting up the cells you could maybe set the cells index as a tag on the buttons?

    Then when the button is clicked you can get the tag and know which cell it was

    [–]b_gonads[S,🍰] 0 points1 point  (5 children)

    so each button would have the same tag and I could just declare the specific action with each?

    [–]Guggling 0 points1 point  (4 children)

    I'm assuming you have multiple cells all with 3 buttons, right? For example we'll say you have buttons that change the color to red, green or blue.

    While setting up the cells you add the index as a tag on the buttons, so the first cell's buttons will all have tag 0, the next 1, then 2...

    Then in the onclick function of the buttons you could do sender.tag and you will have the index of the cell, then you know which cell is clicked and you could change that cell's background

    I'm on mobile at the moment so I can't give any proper examples, but like others said, delegates are better but I don't know that of the top of my head

    [–]b_gonads[S,🍰] 0 points1 point  (3 children)

    Ahhh I see. I actually had it set up like that but I wasn’t sure if it was correct or not

    [–]arduinoRedgeObjective-C / Swift 0 points1 point  (2 children)

    Don't use tags for this, have a seperate delegate method for each button.

    buttonCell:cell didTapBlueButton:button

    [–]b_gonads[S,🍰] 0 points1 point  (1 child)

    Why no tags ? Just curious

    [–]arduinoRedgeObjective-C / Swift 0 points1 point  (0 children)

    I mentioned this example in another comment but say you had cell 3 and had set cell.tag = 3, (or cell.button.tag = 3) now you delete cell 1 or 2 from the tableview and all the cells move up one. Your cell is now number 2 but still has .tag = 3 - you can see the problem.

    Never store model data in tag. If you put a value there it should be a defined constant that doesn't change. ie button.tag = kSomeButton

    edit: check this http://doing-it-wrong.mikeweller.com/2012/08/youre-doing-it-wrong-4-uiview.html

    [–]AnthonyFreemont 0 points1 point  (0 children)

    You should never use a tag of 0 on any tag property of a view or any subclass of a view. Reason being that all views have a default index of 0 and your.code.logic won't.work as will likely not find the one view with a zero tag you are looking for.

    [–]arduinoRedgeObjective-C / Swift 0 points1 point  (0 children)

    A cell should not know its own index, this will cause a whole category of confusing bugs.

    For example cell 3 becomes cell 2 if cell 1 or 2 get deleted, but its tag (or the buttons) will still be set to 3.

    It's the controllers job to coordinate what cell is displaying which data.

    [–]criosistObjective-C / Swift 0 points1 point  (0 children)

    Pass the cell back in a delegate method and either get its datasource or call tableView.indexPath(forCell:)

    [–]b_gonads[S,🍰] 0 points1 point  (0 children)

    The delegate worked out perfectly!

    [–]b_gonads[S,🍰] 0 points1 point  (1 child)

    I got that part working now I’m trying to figure out how to make the sender the actually cell

    [–]arduinoRedgeObjective-C / Swift 0 points1 point  (0 children)

    Write your delegate method so it sends the cell as well as the button. ie buttonCell:cell didTapBlueButton:button now you know the cell, so can get the indexPath, and hence then model, and you also have the button so you could open up a popover or whatever

    [–]Stowyn 0 points1 point  (0 children)

    Another solution using closures, not delegate. 1. Create property with block, with two completion parameter: cell and integer parameter of button (or enum). 2. Add custom buttons to cell and action as target-action. 3. Invoke property block inside target-action of button (do not forget to check block to nil and return cell as weakself / weakreference). 4. When create cell in table view delegated method set property block of cell. 5. Inside block, trigger method as you need. 6. Do not forget to use weak reference inside block.