【问题标题】:Embedded UICollectionView in View Controller not getting called Swift视图控制器中的嵌入式 UICollectionView 没有被调用 Swift
【发布时间】:2016-03-08 00:11:46
【问题描述】:

由于某些奇怪的原因,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!

}

【问题讨论】:

  • 你在哪里设置 collectionView 的数据源和委托

标签: ios swift uicollectionview uicollectionviewcell collectionview


【解决方案1】:

你需要将collection view的datasource设置为controller。

1) 在界面生成器中,只需从集合视图拖动到控制器。并选择委托和数据源..

2) 以编程方式

override func viewDidLoad() {
    super.viewDidLoad()

    channelCollectionView.delegate = self
    channelCollectionView.dataSource = self

   let baseURL = ""
    getJSON(baseURL)
  }

【讨论】:

    【解决方案2】:

    你是否设置了collectionView的delegate和dataSource属性?如果没有,请将您的 viewDidLoad 更改为此,以便以编程方式设置这两个属性:

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

    如果不设置委托和数据源,collectionView 将不知道去哪里调用适当的方法,例如 cellForItemAtIndexPath。希望这能解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 2020-03-21
      • 2016-08-22
      相关资源
      最近更新 更多