首页 > 解决方案 > 仅当条件为真时来自 TableView 的 UIStoryboardSegue

问题描述

我正在 UITableView 上更改我的应用程序。

我正在使用分段控件来确定用户想要查看的内容。他们将查看已售出的列表或有效列表。那是什么listingTypeString成立的。

ViewSingleItem这是我的续集,如果列表是已售出的列表,我只想进入。但是,这有效,当我将控件切换到活动状态时,我不会排序,但是,控制台会出现错误,就像我进入ViewSingleItem. 我怎样才能阻止这种情况发生?如果用户切换到活动状态,我最终希望加载一个网站,但现在,我只希望它打印下面的内容。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(listingTypeString == "sold") {
            if  segue.identifier == "ShowItemDetailsSeque", let destination = segue.destination as? ViewSingleItem, let itemIndex = tableView.indexPathForSelectedRow?.row {
                let selectedItem = self.currentItemsArray[itemIndex]
                destination.theItemId = selectedItem.itemID
             }
        } else {
            print("NOT A SOLD LISTING")
        }
}

更新代码

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return currentItemsArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CurrentItemsCell {
            cell.isUserInteractionEnabled = true
            let responseItems = currentItemsArray[indexPath.row]
            cell.configureCell(currentItem: responseItems)
            cell.mDelegate = self
            cell.setNeedsLayout()
            return cell
        } else {
            return CurrentItemsCell()
        }

    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            //let identifier = banners[indexPath.row].link
            switch listingTypeString!{
            case "sold":
//                let storyboard = UIStoryboard(name: "Main", bundle: nil)
//
//                let destination = storyboard.instantiateViewController(withIdentifier: "ShowItemDetailsSeque") as! ViewSingleItem
//                let itemIndex = tableView.indexPathForSelectedRow?.row
//
//                let selectedItem = self.currentItemsArray[itemIndex!]
//                destination.theItemId = selectedItem.itemID
//
//
//                performSegue(withIdentifier: "ShowItemDetailsSeque", sender: self)

                self.performSegue(withIdentifier: "ShowItemDetailsSeque", sender:self.currentItemsArray[indexPath.row].itemID)

            case "active":
                print("ACTIVE LISTING, EBAY LINK")
            default :
                self.performSegue(withIdentifier: "ShowItemDetailsSeque", sender:self.currentItemsArray[indexPath.row].itemID)
            }

        tableView.deselectRow(at: indexPath, animated: false)
    }



    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//        if(listingTypeString! == "sold") {
//            if  segue.identifier == "ShowItemDetailsSeque", let destination = segue.destination as? ViewSingleItem, let itemIndex = tableView.indexPathForSelectedRow?.row {
//                let selectedItem = self.currentItemsArray[itemIndex]
//                destination.theItemId = selectedItem.itemID
//             }
//        } else {
//            //String affilateLink = SoldForConstants.EBAY_AFFILIATE_LINK.replace("[*]", itemId);
//            //UIApplication.shared.openURL(URL(string: "https://www.soldfor.app/privacy")!)
//            print("GO TO THE EBAY URL LINK")
//        }



        if  segue.identifier == "ShowMoreOptionsSeque", let destination = segue.destination as? ViewMoreOptions {
            destination.mDelegate = self

            destination.searchedFor = itemDescriptionData
            destination.categoryArray = self.currentItemsCategoryArray.removingDuplicates()
        }


        if segue.identifier == "displayExtraImages"  {
            let  vc = segue.destination as! ViewImages
            vc.itemId  = sender as! String
        }



    }

标签: iosswift

解决方案


您需要在 didSelect 方法中执行 segue 以下方法仅用于传输数据或值。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}

这是一个示例您可以将 switch case 替换为 if else

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let identifier = banners[indexPath.row].link
            switch identifier{
            case "SOCIAL":
                performSegue(withIdentifier: "vouchSocial", sender: self)
            case "TRAVEL":
                print("Move to \(identifier!)")
                performSegue(withIdentifier: "vouchTravel", sender: self)
            case "EVENTS":
                print("Move to \(identifier!)")
                performSegue(withIdentifier: "vouchEvents", sender: self)
            case "WHATSAPP":
                print("Move to \(identifier!)")
                performSegue(withIdentifier: "vouchWhatsapp", sender: self)
            default :
                link = banners[indexPath.row].link
                performSegue(withIdentifier: "vouchURL", sender: self)
                print("No Identifier \(String(describing: self.banners[indexPath.row].link))")
            }

        tableView.deselectRow(at: indexPath, animated: false)
    }


推荐阅读