views:

97

answers:

2

I have a bunch of custom UITableCells that I have built in interface builder. These are then associated with their correct cell index and every looks great in my tableView.

Each cell has a few buttons, so rather than creating an outlet for each and every table cell button, is there a way to just "find" any UIButton types within the current cell? I need to do this because I want to give it an IBAction so when you click on the button it does something.

I am just wanting to save code, rather than creating 20 or more IBOutlets.

Ideas?

A: 

I think you could iterate through the subviews property in any UIView, then to check if it's specifically a UIButton you can use - (BOOL)isKindOfClass:(Class)aClass. To differentiate between the UIButtons you can apply tags to them or check other properties.

EDIT: By the way, why not just sub-class UITableViewCell, add your button(s) as IBOutlet properties, then when you want to access them you can just do myCell.someButton ? Here is an article on how to sub-class UITableViewCell and use it in your UITableView.

If you do it this way, then everything will be organized. The UITableView will only worry about its UITableViewCells, and each UITableViewCell will worry about its individual UIButtons.

Jorge Israel Peña
Ok, I tried: for (UIView *subview in cell.subviews) { if ([subview isKindOfClass:[UIButton class]]) { NSLog(@"Found a button"); } }But that did not seem to find any buttons...
Nic Hubbard
Try changing it to: UIView *subview in cell.contentView.subviews
Jorge Israel Peña
That seems to no run the loop at all.
Nic Hubbard
See my edit, it offers an alternative solution.
Jorge Israel Peña
A: 

If you give each a unique tag in Interface Builder, you can find them with the viewWithTag: method of UIView.

filipe