类型的价值'任何?'没有会员标记'

时间:2018-01-11 11:56:03

标签: ios swift uitableview swift4

我在我的自定义tableViewCell上添加了一个按钮,我希望在点击按钮后,它会根据按钮所在的单元格传递数据,以便找出哪个单元格是按钮,我添加了一个标签,但是我收到此错误

enter image description here

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToLast" && sender is IndexPath {


             let dvc = segue.destination as! FinalClass 
            dvc.index = (sender as! IndexPath).row
            dvc.places = sortedArray
            dvc.userLocation = currentLocation

            }
        if segue.identifier == "goToReview" {

            let index3 = IndexPath(row: sender.tag, section: 0)
            let rev = segue.destination as! ReviewClass


        }

}

我该如何调整它?

cell.reviewButton.tag = indexPath.row)这是我在cellForRowAt中添加的标签

更新

这是我VC中的按钮操作

  @IBAction func ReviewButtonTap(_ sender: UIButton) {

      performSegue(withIdentifier: "goToReview" , sender: self)
    }

这是我的自定义tableViewCell类

中的插座
 @IBOutlet weak var reviewButton: UIButton!

我不明白如何将此按钮与

相关联
else if segue.identifier == "goToReview", let button = sender as? UIButton, let rev = segue.destination as? ReviewClass {
            let index3 = IndexPath(row: button.tag, section: 0)

5 个答案:

答案 0 :(得分:1)

强制将可选的Any向下转换为预期的UIButtonIndexPath并使用else子句代替if两次

 if segue.identifier == "goToLast" {
        let dvc = segue.destination as! FinalClass 
        let indexPath = sender as! IndexPath
        dvc.index = indexPath.row
        dvc.places = sortedArray
        dvc.userLocation = currentLocation
}
else if segue.identifier == "goToReview" {
        let button = sender as! UIButton
        let index3 = IndexPath(row: button.tag, section: 0)
        let rev = segue.destination as! ReviewClass
}

或使用switch

 switch segue.identifier {
     case "goToLast":
        let dvc = segue.destination as! FinalClass 
        let indexPath = sender as! IndexPath
        dvc.index = indexPath.row
        dvc.places = sortedArray
        dvc.userLocation = currentLocation

    case "goToReview":
        let button = sender as! UIButton
        let index3 = IndexPath(row: button.tag, section: 0)
        let rev = segue.destination as! ReviewClass

    default: break
}

代码不得崩溃。如果它确实你犯了设计错误。

修改

您必须将UIButton实例(sender)从IBAction传递到sender的{​​{1}}参数。出口是无关紧要的。

performSegue

答案 1 :(得分:1)

在您的方法中,如果它可以是不同的对象,您通常会在其中提供Any发件人。在这种情况下,您需要将其类型转换为特定对象,最好在swift中以let button = sender as? UIButton完成。

通过这样做,您将收到一个可选值,其标记为button?.tag,其类型为Int?,然后再次禁止您创建索引路径。

因此,此方法的正确方法应如下:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToLast", let indexPath = sender as? IndexPath {
            let dvc = segue.destination as! FinalClass 
            dvc.index = indexPath.row
            dvc.places = sortedArray
            dvc.userLocation = currentLocation
    } else if segue.identifier == "goToReview", let button = sender as? UIButton {
            let index3 = IndexPath(row: button.tag, section: 0)
            let rev = segue.destination as! ReviewClass
    }
}

请注意,我已更改了两个if语句,甚至设法删除了额外的强制解包(!内容)。如果您不介意在不正确的情况下强制解包会导致您的应用崩溃,那么您也可以这样做:

let index3 = IndexPath(row: (sender as! UIButton).tag, section: 0) 

但我建议你避免所有的力量展开以提高稳定性。对于您的情况:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToLast", let indexPath = sender as? IndexPath, let dvc = segue.destination as? FinalClass  {
            dvc.index = indexPath.row
            dvc.places = sortedArray
            dvc.userLocation = currentLocation
    } else if segue.identifier == "goToReview", let button = sender as? UIButton, let rev = segue.destination as? ReviewClass {
            let index3 = IndexPath(row: button.tag, section: 0)
    } else {
         print("ERROR: Incorrect configuration!") // Put a breakpoint here and analyze what was the identifier and what the sender so none of the statements were hit
    }
}

答案 2 :(得分:0)

您可以从发件人处获取UIButton对象,然后UIButton对象具有要访问的标记属性。

let button = sender as? UIButton
let index3 = IndexPath(row: button?.tag ?? 0, section: 0)

答案 3 :(得分:0)

您需要使用以下方法更改您的segue方法

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToLast" && sender is IndexPath {


         let dvc = segue.destination as! FinalClass 
        dvc.index = (sender as! IndexPath).row
        dvc.places = sortedArray
        dvc.userLocation = currentLocation

        }
    if segue.identifier == "goToReview" {

        let index3 = IndexPath(row: (sender as! UIButton).tag, section: 0)
        let rev = segue.destination as! ReviewClass


    }
}

答案 4 :(得分:0)

首先要管理这个问题,首先需要在单元格中为行已经完成分配按钮标记。

cell.reviewButton.tag = indexPath.row

之后,您可以在行的单元格中的按钮上添加目标操作

cell.reviewButton.addTarget(self, action:#selector(self. 
func_Navigate(_:)), for:UIControlEvents.touchUpInside)

用于行的外侧单元格实现如下功能: -

@IBAction func func_Navigate(_ sender: UIButton)
{

If sender.tag == 0

 {

    \\ here sender.tag = 0 means button belong from fist row of your table view. you can perform navigation like that :-
    self.performSegue(withIdentifier: "goToLast", sender: self)

}
else If sender.tag == 1

{
    \\ here sender.tag = 1 means button belong from second row of your table view. you can perform navigation like that :-
    self.performSegue(withIdentifier: "goToReview", sender: self)
}