material-components-ios
material-components-ios copied to clipboard
[MDCCard] in UITableViewCell
Hello,
I have added MDCCard in table cells. Now, how can I get NSIndexPath on a touch event?
The title doesn't have a [Component] prefix.
One workaround solution I got is :
- In your cellForRowAtIndexPath method,find the MDCCard view
- Using addTarget method to add UIControlEventTouchUpInside event like this
[cardView addTarget:self action:@selector(MDCCardDidClickRow:) forControlEvents:UIControlEventTouchUpInside]; - In selector method you can find the super view of MDCCard and get the NSIndexPath:
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[contentView superview]]; - You can do what you want with indexPath
One workaround solution I got is :
- In your cellForRowAtIndexPath method,find the MDCCard view
- Using addTarget method to add UIControlEventTouchUpInside event like this
[cardView addTarget:self action:@selector(MDCCardDidClickRow:) forControlEvents:UIControlEventTouchUpInside];- In selector method you can find the super view of MDCCard and get the NSIndexPath:
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[contentView superview]];- 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) }