gpt4 book ai didi

ios - View Controller 中的嵌入式 UICollectionView 未被调用 Swift

转载 作者:行者123 更新时间:2023-11-29 01:10:10 25 4
gpt4 key购买 nike

由于某些奇怪的原因,未调用 collectionView。我在 numberOfItemsInSection 和 cellForItemAtIndexPath 处放置了断点,但它们从未被调用过。这是我的代码:

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 Cell 子类化的单元格:

class ChannelCell : UICollectionViewCell {

@IBOutlet var channelImageView: UIImageView!

}

最佳答案

您需要将 Collection View 的数据源设置为 Controller 。

1) 在界面构建器中,只需从 Collection View 拖动到 Controller 。并选择委托(delegate)和数据源..

enter image description here enter image description here

2) 程序化

override func viewDidLoad() {
super.viewDidLoad()

channelCollectionView.delegate = self
channelCollectionView.dataSource = self

let baseURL = ""
getJSON(baseURL)
}

关于ios - View Controller 中的嵌入式 UICollectionView 未被调用 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35856630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com