使用 UITableView 时发生不需要的 segue



我有一个UItableView,我执行一个UIView的续集。所显示UIView的信息因UITableView中选择的行而异。UIView完美呈现。问题是,当显示新UIView时,如果我单击屏幕上初始行所在的同一位置,则 然后我又回到UITableView

我在故事板上做了一个叫做dayx的续集。然后,我使用以下代码来呈现新UIView。这是我用来执行 segue 的代码:

  //when row is pressed what happens
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.destination as! foodinfo
    destinationVC.counter = rowselected
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    rowselected = indexPath.row
    self.performSegue(withIdentifier: "dayx", sender: self)
    //making desleected row not highlighted
    tableView.deselectRow(at: indexPath, animated: true)
}

呈现的UIView代码如下:

类食品信息: UIViewController {

var counter = Int()
let playerController = AVPlayerViewController()
@IBOutlet var tabs: UISegmentedControl!
@IBOutlet var theTitleLable: UILabel!
@IBOutlet var stack: UIStackView!
@IBOutlet var ScrollView: UIScrollView!
@IBOutlet var foodImageview: UIImageView!
@IBOutlet var navBar: UINavigationBar!

var simpleViewX: UIView!
var simpleViewY: UIView!
var theTitleArray = ["title1","title2","title3","title4","title5","title6","title7"]
var theImageArray = ["image1", "image2", "image3","image4","image5",image6","image7"]

//video links
var calledVideo: String?
let vids = ["https://vimeo.com/123456789","https://vimeo.com/123456789", "https://vimeo.com/123456789", "https://vimeo.com/123456789","https://vimeo.com/123456789","https://vimeo.com/123456789","https://vimeo.com/123456789"]

override func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(self.deviceOrientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
    getTitle()
    getImage()
    getNavbar()
    //Different subViews for ingredients and steps
    if counter == 0 {
        simpleViewX = SimpleVC0().view
        simpleViewY = SimpleVC1().view
        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)
        simpleViewY.isHidden = true
    }
    if counter == 1 {
        simpleViewX = SimpleVC2().view
        simpleViewY = SimpleVC3().view
        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)
        simpleViewY.isHidden = true
    }
    if counter == 2 {
        simpleViewX = SimpleVC4().view
        simpleViewY = SimpleVC5().view
        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)
        simpleViewY.isHidden = true
    }
    if counter == 3 {
        simpleViewX = SimpleVC6().view
        simpleViewY = SimpleVC7().view
        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)
        simpleViewY.isHidden = true
    }
    if counter == 4 {
        simpleViewX = SimpleVC8().view
        simpleViewY = SimpleVC9().view
        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)
        simpleViewY.isHidden = true
    }
    if counter == 5 {
        simpleViewX = SimpleVC10().view
        simpleViewY = SimpleVC11().view
        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)
        simpleViewY.isHidden = true
    }
    if counter == 6 {
        simpleViewX = SimpleVC12().view
        simpleViewY = SimpleVC13().view
        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)
        simpleViewY.isHidden = true
    }
}

func getTitle() {
    theTitleLable.text = theTitleArray[counter]
}
func getImage() {
    foodImageview.image = UIImage(named: theImageArray[counter] + ".jpg")
}
func getNavbar () {
    self.navBar.setBackgroundImage(UIImage(), for: .default)
    self.navBar.shadowImage = UIImage()
}

@IBAction func tabselected(_ sender: Any) {
    switch (sender as AnyObject).selectedSegmentIndex {
    case 0:
        //shiftView.bringSubviewToFront(simpleViewX)
        simpleViewY.isHidden = true
        simpleViewX.isHidden = false
        break
    case 1:
        //shiftView.bringSubviewToFront(simpleViewY)
        simpleViewX.isHidden = true
        simpleViewY.isHidden = false
        break
    case 2:
        //calledvideo in array is the value of the counter.
        calledVideo = vids[counter]
        geturl()

        break
    default:
        break
    }
}
//Getting url info for video by using exractor
func geturl() {
    if let url = URL(string: calledVideo!) {
        HCVimeoVideoExtractor.fetchVideoURLFrom(url: url, completion: { ( video:HCVimeoVideo?, error:Error?) -> Void in
            if let err = error {
                DispatchQueue.main.async() {
                    let alert = UIAlertController(title: "Error", message: err.localizedDescription, preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                    self.present(alert, animated: true, completion: nil)
                }
                return
            }
            guard let vid = video else {
                print("Invalid video object")
                return
            }

            //play video
            let player = AVPlayer(url: vid.videoURL[.Quality540p]!)
            self.playerController.player = player
            self.present(self.playerController, animated: true) {
                player.play()
            }
        })
    }
}
//ROTATION OF VIDEO BUT NOT ALLOWING VC TO ROTATE---this is the guy you got-----
@objc func deviceOrientationDidChange() {
    switch UIDevice.current.orientation {
    case .faceUp, .faceDown, .portrait, .unknown, .portraitUpsideDown:
        // default the player to original rotation
        self.playerController.view.transform = .identity
        self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
    /*case .landscapeLeft:
       print("Landscape left")
       self.playerController.view.transform = CGAffineTransform(rotationAngle: CGFloat((90 * Double.pi)/180))
        self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)*/
    case .landscapeRight, .landscapeLeft:
                  print("Landscape right")
                  self.playerController.view.transform = CGAffineTransform(rotationAngle: CGFloat((-90 * Double.pi)/180))
        self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
    }
}
//---------------------------------------

}

我不知道为什么它会回到UITableView.有人知道我做错了什么吗?

您提到您正在使用情节提要,并且您已经将两个视图UITableView和目标UIView与带有identifier"dayx"的 segue 连接起来。

您覆盖tableView(didSelectRowAt:)有什么特定原因吗?

您是否考虑或尝试使用与此类似的代码(并删除tableView(didSelectRowAt:)(...?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier = "dayx" {
        if let rowSelected = tableView.indexPathForSelectedRow {
            let destinationVC = segue.destination as! foodinfo
            destinationVC.counter = rowSelected
        }
    }
}

还要设置以下默认属性(在 viewWillAppear(animated) 中(以适合您的 UI... clearsSelectionOnViewWillAppear = true(或假(。

最新更新