0

由於某些奇怪的原因,collectionView未被調用。我在numberOfItemsInSection和cellForItemAtIndexPath中放置了斷點,但它們從不被調用。這裏是我的代碼:View Controller中的嵌入式UICollectionView未被調用Swift

class ChannelViewController: UIViewController, UISearchResultsUpdating, UICollectionViewDataSource, UICollectionViewDelegate { 

    var channelArray: [ChannelInfo] = [] 
    var filteredSearchResults = [ChannelInfo]() 
    var resultsSearchController = UISearchController(searchResultsController: nil) 
    var logosShown = [Bool](count: 50, repeatedValue: false) 
    var detailUrl: String? 
    var apiKey = "" 
    var channel: String! 
    var channelForShow: String! 
    var task: NSURLSessionTask? 

    @IBOutlet var channelCollectionView: UICollectionView! 

    override func viewDidLoad() { 

    let baseURL = "" 
    getJSON(baseURL) 
    } 


    //MARK: CollectionView 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    if resultsSearchController.active && resultsSearchController.searchBar.text != "" 
    { 
     return filteredSearchResults.count 
    } else { 
     return channelArray.count 
    } 
    } 


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ChannelCell", forIndexPath: indexPath) as! ChannelCell 
    let channel: String 
    if resultsSearchController.active && resultsSearchController.searchBar.text != "" { 
     channel = self.filteredSearchResults[indexPath.row].logo 
    } else { 
     channel = self.channelArray[indexPath.row].logo 
    } 
    return cell 
    } 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

    var replacedTitle: String? 
    if resultsSearchController.active && resultsSearchController.searchBar.text != "" { 

     channelForShow = filteredSearchResults[indexPath.row].channelName 

     } 
    } else { 
     channelForShow = self.channelArray[indexPath.row].channelName 
     } 
    } 
    self.dismissSearchBar() 
    performSegueWithIdentifier("channelCollectToShowSegue", sender: self) 
    } 

}

我也有子類爲的CollectionView小區的小區:

class ChannelCell : UICollectionViewCell { 

    @IBOutlet var channelImageView: UIImageView! 

} 
+1

你在哪裏設置數據源和代理的collectionView – bolnad

回答

1

你需要收集視圖的數據源設置到控制器。

1)在界面構建器中,只需從集合視圖拖動到控制器即可。並同時選擇委託和數據源..

enter image description here enter image description here

2)編程方式

override func viewDidLoad() { 
    super.viewDidLoad() 

    channelCollectionView.delegate = self 
    channelCollectionView.dataSource = self 

    let baseURL = "" 
    getJSON(baseURL) 
    } 
0

想通了,忘了委託和數據源連接至情節板中。

+0

@Earl Gray發佈了更深入的解釋這個答案,爲了幫助有同樣問題的未來用戶,你應該標記他的答案是正確的。 –

0

您是否設置了collectionView的委託和數據源屬性?如果沒有,你的viewDidLoad改變這一點,以編程方式設置這兩個屬性:

override func viewDidLoad() { 
    channelCollectionView.delegate = self 
    channelCollectionView.dataSource = self 
    let baseURL = "" 
    getJSON(baseURL) 
} 

如果不設置委託和數據源的的CollectionView不知道到哪裏調用適當的方法如cellForItemAtIndexPath。希望這能解決你的問題。

相關問題