【问题标题】:Selector In Swift 3.0Swift 3.0 中的选择器
【发布时间】:2016-12-26 06:20:09
【问题描述】:

我对 Swift 3.0 有疑问

在 Objective-C 中,您可以像这样为 selector 声明一个属性

@property (nonatomic,assign)SEL ItemSelected;

但是如何在 Swift 3.0 中将选择器声明为属性,因为我想在其他类中使用此属性并且应该在该类中执行操作。

我正在尝试这样使用它(在 tableview 单元格中声明):

var itemSelected = #selector(selctedItem(_ :))

在 viewcontroller 表格视图单元格中使用它

cell.itemSelected(target: self, action: HomeViewController.self.selctedItem(_ :))

报错

使用未声明的item selectedItem

我不确定如何在带有 selctor 的 tableview 中使用。

【问题讨论】:

    标签: ios swift swift3 performselector


    【解决方案1】:

    你可以像这样在 Swift 3 中声明选择器。

    var itemSelected: Selector?
    

    或者

    var itemSelected = #selector(tapGesture) 
    

    稍后您可以使用上面的itemSelected 选择器进行这样的操作。

    例如:

    let tap = UITapGestureRecognizer(target: self, action: itemSelected)
    

    tapGesture 声明为

    func tapGesture(_ sender: UITapGestureRecognizer) { }
    

    编辑:您已将collectionView 添加到您的TableViewCell 中,因此要获得CollectionViewCell 的选定IndexPath,请声明一个协议并将其与您的tableViewCell 一起使用。

    protocol SelectedCellDelegate {
         func getIndexPathOfSelectedCell(tableIndexPath: IndexPath, collectionViewCell indexPath: IndexPath) 
    }
    

    现在在您的 CustomTableViewCell 中创建一个 SelectedCellDelegate 实例和一个 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 方法中设置 delegatetableCellIndexPath

    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)") 
    }
    

    【讨论】:

    • 如果我不喜欢将参数 ItemSelected 放入它的给出错误,如 unresolved identififer 。如果我声明 var selector = #selector() 它给出错误
    • @IOS_PROGRAMMER 首先声明它,例如可选var itemSelected: Selector?,然后设置它的值。
    • 如果我将其声明为可选,则说明不能调用非函数类型
    • @IOS_PROGRAMMER 使用您正在尝试的新代码编辑您的问题。
    • @IOS_PROGRAMMER 你在这里没有意义,你已经在 tableView 单元格中声明它并像@​​987654340@ 一样使用它,你想首先明确这一点。
    猜你喜欢
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    相关资源
    最近更新 更多