我需要有条件的Segue。可能的选项是:

如果链接具有子链接->我应该只重新加载UICollectionView。
如果该链接没有子链接->转到其他窗口。
这是我的代码。

override func viewDidLoad() {
super.viewDidLoad()
self.loaddata(self.url) }

func loaddata(url: String){
   var loaded: Bool = false
   api.remoteUrl = url + self.end_url
   println(api.remoteUrl)

   api.getData({data, error -> Void in
    println("entro aqui")
    if (data != nil){
        println(data)
        // Fix possible error if no "results" key
        if let results = data["results"].array {
            self.delete_subcategory_withoutvideos(results)
        }
        dispatch_async(dispatch_get_main_queue()) {
            self.collectionView.reloadData()
            //self.colle  ctionView(<#collectionView: UICollectionView#>cellForItemAtIndexPath: <#NSIndexPath#>)

        }
        println("Data reloaded")
    } else {
        println("api.getData failed")
        println(error)
    }
    //println(self.result_category)
    loaded = true
    self.collectionView.reloadData()

})

while (!loaded){}
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   let cell = sender as! UICollectionViewCell
    let index = self.collectionView!.indexPathForCell(cell) // this return -> NSIndexPath?
   if (self.result_category[index!.row]["children"].string != nil){
     self.loaddata(self.result_category[index!.row]["children"].string!)
   }else{
      let vc : VideosViewController = segue.destinationViewController as! VideosViewController
       vc.id_category =  self.result_category[index!.row]["id"].intValue
       println(vc.id_category)}

}

最佳答案

您需要按住Control并拖动以从视图控制器而不是实际的集合视图单元格创建序列,然后实施didSelectItemAtIndexPath

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    if (self.result_category[indexPath.row]["children"].string != nil){
        self.loaddata(self.result_category[indexPath.row]["children"].string!)
    } else{
      self.performSegueWithIdentifier("<< YOUR SEGUE's NAME GOES HERE >>", sender: collectionView.cellForItemAtIndexPath(indexPath))
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let cell = sender as? UICollectionViewCell, let indexPath = self.collectionView?.indexPathForCell(cell) {
        let vc : VideosViewController = segue.destinationViewController as! VideosViewController
        vc.id_category =  self.result_category[indexPath.row]["id"].intValue
        println(vc.id_category)
    }
}

关于ios - 如何有条件地进行快速搜索,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31639299/

10-11 17:13