Search code examples
uitableviewuiaccessoryview

Setting a Button with an image in the AccessoryView


I would like to customize the image in the accessory views of some tables while allowing to pass the table and indexPath to the accessoryButtonTappedForRowWithIndexPath callback including the clicked table. I used to employ the UIControl+Blocks.h class here reported:

 @implementation UIControl (Blocks)
 - (void)addEventHandler:(ActionBlock)handler forControlEvents:  (UIControlEvents)controlEvents
{
    objc_setAssociatedObject(self, &UIButtonHandlerKey, handler, OBJC_ASSOCIATION_COPY_NONATOMIC);
   [self addTarget:self action:@selector(callActionHandler:event:)   forControlEvents:controlEvents];
}
- (void)callActionHandler:(id)sender event:(id)event
{
   ActionBlock handler = (ActionBlock)objc_getAssociatedObject(self, &UIButtonHandlerKey);
   if (handler) {
       handler(sender, event);
   }
}
@end

But it started behaving weirdly by sometimes returning the button in the place of the event so crashing my app when I try to get allTouches and so I am fervidly looking for a replacement.

I also tried replacing the original (i) accessory button, but in this way I also lose the accessibilityLabel. Please help.


Solution

  • I restored the old solution by foreseeing a table reload in the case the buttons get crazy and start sending themselves as the control event.