material-components-ios icon indicating copy to clipboard operation
material-components-ios copied to clipboard

[MDCCard] in UITableViewCell

Open maulikpat opened this issue 5 years ago • 3 comments

Hello,

I have added MDCCard in table cells. Now, how can I get NSIndexPath on a touch event?

maulikpat avatar Sep 12 '20 15:09 maulikpat

The title doesn't have a [Component] prefix.

One workaround solution I got is :

  1. In your cellForRowAtIndexPath method,find the MDCCard view
  2. Using addTarget method to add UIControlEventTouchUpInside event like this [cardView addTarget:self action:@selector(MDCCardDidClickRow:) forControlEvents:UIControlEventTouchUpInside];
  3. In selector method you can find the super view of MDCCard and get the NSIndexPath: NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[contentView superview]];
  4. You can do what you want with indexPath

GaoShin avatar Dec 23 '20 06:12 GaoShin

One workaround solution I got is :

  1. In your cellForRowAtIndexPath method,find the MDCCard view
  2. Using addTarget method to add UIControlEventTouchUpInside event like this [cardView addTarget:self action:@selector(MDCCardDidClickRow:) forControlEvents:UIControlEventTouchUpInside];
  3. In selector method you can find the super view of MDCCard and get the NSIndexPath: NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[contentView superview]];
  4. You can do what you want with indexPath

Swift version of this:

In TableViewController's CellForRowAt: cell.cardView.addTarget(self, action: #selector(MDCCardDidClickRow(sender:)), for: .touchUpInside)

selectorMethod: (may change depending on how you setup your views) @objc func MDCCardDidClickRow(card: MDCCard) { let cellClass = card.superview?.superview as! MyUITableViewCell let indexPath = self.tableView.indexPath(for: cellClass) print(ip?.row) }

mhmmdmz avatar Dec 29 '20 02:12 mhmmdmz