Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 3.0中的选择器_Ios_Swift_Swift3_Performselector - Fatal编程技术网

Ios Swift 3.0中的选择器

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

我对Swift 3.0有疑问

在Objective-C中,您可以像这样声明
选择器的属性

@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像第一个一样声明它,例如optional
var itemSelected:Selector?
,而后者设置它的值。如果我将它声明为Operational,它说明不能调用non-functiontype@IOS_PROGRAMMER用您正在尝试的新代码编辑您的问题。@IOS\u程序员您在这里没有意义,您已经在tableView单元格中声明了它,并像使用
HomeViewController.self.selctedItem(:)
首先要实现什么清除它。