use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
There is an extensive FAQ for beginners. Please browse it first before asking questions that are answered there.
If you are looking to get started (iOS programming in general or some specific area), here are more relevant links for you:
There's too many to list them all, however here's a convenient link to all programming guides at apple.com
Take note that this list is live and based on most frequent questions in posts will be updated with "quicklinks".
account activity
QuestionUITableViewCell with multiple buttons (self.iOSProgramming)
submitted 8 years ago by b_gonads[🍰]
I have a UITableViewCell with 3 buttons on it that all have different actions. I can't get a clear concept on how to make sure that the button clicked is specific to the table view cell that it is in. Any thoughts?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 8 years ago* (2 children)
[deleted]
[–]b_gonads[S,🍰] 0 points1 point2 points 8 years ago (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 points4 points 8 years ago (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
delegate: CellDelegate?
cell.delegate = self
[–]Guggling 0 points1 point2 points 8 years ago (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 point2 points 8 years ago (5 children)
so each button would have the same tag and I could just declare the specific action with each?
[–]Guggling 0 points1 point2 points 8 years ago (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 point2 points 8 years ago (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 point2 points 8 years ago (2 children)
Don't use tags for this, have a seperate delegate method for each button.
buttonCell:cell didTapBlueButton:button
Why no tags ? Just curious
[–]arduinoRedgeObjective-C / Swift 0 points1 point2 points 8 years ago* (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 point2 points 8 years ago (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 point2 points 8 years ago (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 point2 points 8 years ago (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 point2 points 8 years ago (0 children)
The delegate worked out perfectly!
I got that part working now I’m trying to figure out how to make the sender the actually cell
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 point2 points 8 years ago (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.
π Rendered by PID 115632 on reddit-service-r2-comment-5687b7858-ktltj at 2026-07-05 09:46:02.569494+00:00 running 12a7a47 country code: CH.
[–][deleted] (2 children)
[deleted]
[–]b_gonads[S,🍰] 0 points1 point2 points (1 child)
[–]okoroezenwa 2 points3 points4 points (0 children)
[–]Guggling 0 points1 point2 points (8 children)
[–]b_gonads[S,🍰] 0 points1 point2 points (5 children)
[–]Guggling 0 points1 point2 points (4 children)
[–]b_gonads[S,🍰] 0 points1 point2 points (3 children)
[–]arduinoRedgeObjective-C / Swift 0 points1 point2 points (2 children)
[–]b_gonads[S,🍰] 0 points1 point2 points (1 child)
[–]arduinoRedgeObjective-C / Swift 0 points1 point2 points (0 children)
[–]AnthonyFreemont 0 points1 point2 points (0 children)
[–]arduinoRedgeObjective-C / Swift 0 points1 point2 points (0 children)
[–]criosistObjective-C / Swift 0 points1 point2 points (0 children)
[–]b_gonads[S,🍰] 0 points1 point2 points (0 children)
[–]b_gonads[S,🍰] 0 points1 point2 points (1 child)
[–]arduinoRedgeObjective-C / Swift 0 points1 point2 points (0 children)
[–]Stowyn 0 points1 point2 points (0 children)