I have doubt in Swift 3.0
In Objective-C you can declare a property for selector
like this
@property (nonatomic,assign)SEL ItemSelected;
But how to declare a selector in Swift 3.0 as property because I would like to use this property in other class and should give action in that class.
I am trying to use it like this (Declared in tableview cell):
var itemSelected = #selector(selctedItem(_ :))
Using it in viewcontroller table view cell
cell.itemSelected(target: self, action: HomeViewController.self.selctedItem(_ :))
it's giving error
use of Undeclared item selectedItem
I am not sure how to use in tableview with selctor.
You can declare selector in Swift 3 like this.
var itemSelected: Selector?
Or
var itemSelected = #selector(tapGesture)
Later you can use above itemSelected
selector with action like this.
For eg:
let tap = UITapGestureRecognizer(target: self, action: itemSelected)
And tapGesture
is declared like
func tapGesture(_ sender: UITapGestureRecognizer) { }
Edit: You have added collectionView
inside your TableViewCell
, so to get the selected IndexPath of CollectionViewCell
, declare one protocol and use it with your tableViewCell.
protocol SelectedCellDelegate {
func getIndexPathOfSelectedCell(tableIndexPath: IndexPath, collectionViewCell indexPath: IndexPath)
}
Now create one instance of SelectedCellDelegate and instance of IndexPath
within your CustomTableViewCell.
class CustomTableCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
//All outlet
var delegate: SelectedCellDelegate?
var tableCellIndexPath = IndexPath()
//CollectionViewDataSource method
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.delegate?.getIndexPathOfSelectedCell(tableIndexPath: tableCellIndexPath, indexPath: indexPath)
}
}
Now inside your ViewController where you have added TableView implement the protocol SelectedCellDelegate
and set the delegate
and tableCellIndexPath
in cellForRowAt indexPath
method.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SelectedCellDelegate {
//your methods
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell // Initialize the cell with your custom TableCell
cell.delegate = self
cell.tableCellIndexPath = indexPath
return cell
}
Now add the delegate method in your ViewController.
func getIndexPathOfSelectedCell(tableIndexPath: IndexPath, collectionViewCell indexPath: IndexPath) {
print("TableView cell indexPath - \(tableIndexPath)")
print("CollectionView cell indexPath - \(indexPath)")
}