由於某些奇怪的原因,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!
}
你在哪裏設置數據源和代理的collectionView – bolnad