2015-07-26 5 views
0

Мне нужно иметь условный Segue. Возможные варианты:как сделать условный segue в swift

Если ссылка имеет дочернюю ссылку -> Мне нужно перезагрузить 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)} 

} 

ответ

1

Вы должны контролировать перетаскивание, чтобы создать SEGUE от View Controller вместо фактического представления коллекции клетки, а затем реализовать 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) 
    } 
} 
Смежные вопросы