将参数传递给 Swift 中的选择器函数

Pass parameter to selector function in Swift

我想将 参数 传递给 定时器 [=39] 中称为 选择器 的函数=].特别是对 cell 的引用,因此我可以在 UI.

中更新一些内容

所以我想要这样的东西:

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(cell: cell), userInfo: nil, repeats: true)

函数

func downloadTimer(cell: InnerCollectionCell) {
    cell.progressBar.setProgress(downloadProgress, animated: true)
}

虽然我可能有点侄女假设这可以做到?

------ 编辑 ------

按照以下示例,但没有像往常一样从单元格中获得预期结果

let innerCell: InnerCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierInner, for: indexPath) as! InnerCollectionCell

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(_:)), userInfo: innerCell, repeats: true)


func downloadTimer(_ timer: Timer) {

    let cell = timer.userInfo

    cell. // no options as expected of a cell

}

如果数据发送正确,我希望有更多这样的选项:

您在创建定时器时将您想要的数据作为 userInfo 参数传入:

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(_:), userInfo: myData, repeats: true)

并使回调看起来像:

func downloadTimer(_ timer: Timer) {
    // access timer.userInfo here
}

使用用户信息设置您的计时器

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(cell: cell), userInfo: data, repeats: true)

并获取用户信息如下

func downloadTimer(_ timer: Timer) {
   let data = timer.userInfo
}

------ 编辑 ------

按照以下示例,但没有像往常一样从单元格中获得预期结果

let innerCell: InnerCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierInner, for: indexPath) as! InnerCollectionCell

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(_:)), userInfo: innerCell, repeats: true)


func downloadTimer(_ timer: Timer) {

    let cell = timer.userInfo as! InnerCollectionCell

    cell. // no options as expected of a cell

}