UITableView Pointer Support for iPad using Objective-C

In iPadOS 13.4, Apple introduced the Mouse support for iPads. Prior to this mouse support had kind-of worked with iPads – I’d tried connecting a USB mouse using the Camera adaptor – but it was a bit flaky and didn’t really provide any feedback on what was clickable.

Fast-forward to 13.4, and now we had a circular pointer and feedback when the pointer was over a clickable item.

For most controls, it was simply a case of adding isPointerInteractionEnabled=yes to the control. However, for some other controls, such as UITableCellView, some custom code was required.

When I attempted to add this to iSMARTtrain for iOS, most, if not all, of the code I found was for Swift. It took a bit of time figuring out how to convert this to Objective-C (and I wasn’t about to rewrite an entire class just to for pointer support!). Here’s the code in case it helps anyone else.

For UITableView

In the View Controller, add the delegate:

@interface YFTListViewTableController : UITableViewController <UIPointerInteractionDelegate>

In the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method, add

if (@available(iOS 13.4, *)) {
  if (cell.interactions.count == 0) {
    UIPointerInteraction *pointerInteraction = [[UIPointerInteraction alloc] initWithDelegate:self];
    [cell addInteraction:pointerInteraction];
  }
}

Where cell is the cell for the row

Add the delegate method to the class

- (UIPointerStyle *)pointerInteraction:(UIPointerInteraction *)interaction styleForRegion:(UIPointerRegion *)region  API_AVAILABLE(ios(13.4)){
    UIPointerStyle *pointerStyle = nil;
    
    UIView *interactionView = interaction.view;
    if (interactionView) {
        UITargetedPreview *targetPreview = [[UITargetedPreview alloc] initWithView:interactionView];
        
        
        UIPointerEffect *hoverEffect = [UIPointerHoverEffect effectWithPreview:targetPreview];
        
        pointerStyle = [UIPointerStyle styleWithEffect:hoverEffect shape:nil];
    }
    return pointerStyle;
}

2 thoughts on “UITableView Pointer Support for iPad using Objective-C”

  1. Thanks for posting; this was very useful! I did note one problem, each time you reuse a tablecell, it looks like you’re going to duplicate the pointerInteraction, so adding a check would be good:

    if (@available(iOS 13.4, *))
    if (cell.interactions.count == 0) {
    [cell addInteraction: [[UIPointerInteraction alloc] initWithDelegate:self]];
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *