How to use UITableView inside UIScrollView and receive a cell click?

前端 未结 6 2344
野趣味
野趣味 2020-12-01 15:43

I have DiscountListTableViewController that is shown as a separate screen in my app. But there\'s another screen (PlaceDetailsViewController) where

6条回答
  •  无人及你
    2020-12-01 16:18

    Will share how solved that. Did subclassed UIScrollView and used as container view in PlaceDetailsViewController:

    @interface PlaceDetailsContainerUIScrollView : UIScrollView
    
    @end
    
    @implementation PlaceDetailsContainerUIScrollView
    
    - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
      UIView *result = [super hitTest:point withEvent:event];
    
      if ([result isKindOfClass:[UIView class]] && (result.tag == kDiscountListTableViewCellTag)
      {
        UITableView *tableView = (UITableView *) [[result.superview superview] superview];
    
        return tableView;
    
      }
    
      return result;
    }
    @end
    

    Also, don't forget to set PlaceDetailsContainerUIScrollView.delaysContentTouches = YES and PlaceDetailsContainerUIScrollView.canCancelContentTouches = NO.

    Also, needed small fix in DiscountListTableViewController method:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
      DiscountDetailsViewController *discountDetailsVC = [[DiscountDetailsViewController alloc] init];
    
      //[self.navigationController pushViewController:discountDetailsVC animated:YES];   
      // self.navigationController is nill because it's not pushed on nav stack, so will grab the current one:
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
      [appDelegate.navigationController pushViewController:discountDetailsVC animated:YES];
    }
    

提交回复
热议问题