Ios Swift 3.0中的选择器
我对Swift 3.0有疑问 在Objective-C中,您可以像这样声明Ios Swift 3.0中的选择器,ios,swift,swift3,performselector,Ios,Swift,Swift3,Performselector,我对Swift 3.0有疑问 在Objective-C中,您可以像这样声明选择器的属性 @property (nonatomic,assign)SEL ItemSelected; 但是如何在Swift 3.0中将选择器声明为属性,因为我希望在其他类中使用此属性,并且应该在该类中执行操作 我尝试这样使用它(在tableview单元格中声明): 在viewcontroller表视图单元中使用它 cell.itemSelected(target: self, action: HomeViewCont
选择器的属性
@property (nonatomic,assign)SEL ItemSelected;
但是如何在Swift 3.0中将选择器声明为属性,因为我希望在其他类中使用此属性,并且应该在该类中执行操作
我尝试这样使用它(在tableview单元格中声明):
在viewcontroller表视图单元中使用它
cell.itemSelected(target: self, action: HomeViewController.self.selctedItem(_ :))
这是一个错误
使用未声明的项目selectedItem
我不知道如何在tableview中使用selector。您可以像这样在Swift 3中声明选择器
var itemSelected: Selector?
或
var itemSelected = #selector(tapGesture)
稍后,您可以使用上面的itemSelected
选择器执行以下操作
var itemSelected: Selector?
例如:
let tap = UITapGestureRecognizer(target: self, action: itemSelected)
并且tappostate
声明为
func tapGesture(_ sender: UITapGestureRecognizer) { }
编辑:您已在TableViewCell
中添加了collectionView
,因此要获取所选的CollectionViewCell
的IndexPath,请声明一个协议并将其用于TableViewCell
protocol SelectedCellDelegate {
func getIndexPathOfSelectedCell(tableIndexPath: IndexPath, collectionViewCell indexPath: IndexPath)
}
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)
}
}
现在在CustomTableViewCell中创建SelectedCellDelegate的一个实例和IndexPath
的实例
protocol SelectedCellDelegate {
func getIndexPathOfSelectedCell(tableIndexPath: IndexPath, collectionViewCell indexPath: IndexPath)
}
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)
}
}
现在,在添加了TableView的ViewController中,实现协议SelectedCellDelegate
,并在cellForRowAt indexPath
方法中设置delegate
和tableCellIndexPath
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
}
现在在ViewController中添加委托方法
func getIndexPathOfSelectedCell(tableIndexPath: IndexPath, collectionViewCell indexPath: IndexPath) {
print("TableView cell indexPath - \(tableIndexPath)")
print("CollectionView cell indexPath - \(indexPath)")
}
如果我不喜欢把param ItemSelected放在它的给定错误上,比如unresolved identifier,该怎么办。如果我声明var selector=#selector()它的给予error@IOS_PROGRAMMER像第一个一样声明它,例如optionalvar itemSelected:Selector?
,而后者设置它的值。如果我将它声明为Operational,它说明不能调用non-functiontype@IOS_PROGRAMMER用您正在尝试的新代码编辑您的问题。@IOS\u程序员您在这里没有意义,您已经在tableView单元格中声明了它,并像使用HomeViewController.self.selctedItem(:)
首先要实现什么清除它。