【问题标题】:UICollectionView reloadData - EXC_BREAKPOINTUICollectionView reloadData - EXC_BREAKPOINT
【发布时间】:2016-12-09 06:33:03
【问题描述】:

我的UITableView 的标题中有一个UICollectionView。如果没有数据,我会删除 collectionView。 我从“self.collectionView.reloadData()”行收到 EXC_BREAKPOINT 崩溃

var reccomend: [JSON]? = []

 func loadReccomend(){
            YazarAPI.sharedInstance.loadReccomendedArticles({ reccomend in
                if let data = reccomend["articles"].arrayValue as [JSON]?{
                    self.reccomend = data
                    if (self.reccomend?.count)! < 1 {
                        self.tableView.tableHeaderView = nil
                    }else{
                        self.collectionView.reloadData()
                    }
                }
            })
        } 

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
                return 1
            }

            func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                return self.reccomend?.count ?? 0
            }

            func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
                let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ReccomendCell", forIndexPath: indexPath) as! ReccomendedCollectionViewCell
                cell.article = self.reccomend?[indexPath.row]
                return cell
            }

print(data) 的结果:

[{
  "title" : "title0",
  "author_email" : null,
  "author_id" : 1884,
  "read_count" : 0,
  "is_favorite" : false,

}, {
  "title" : "title1",
  "author_email" : null,
  "author_id" : 1884,
  "read_count" : 0,
  "is_favorite" : false,

}]

【问题讨论】:

  • 你如何将 collectionView 作为 tableView 标题返回?你在 viewForHeaderInSection 中也处理过这些情况吗???

标签: ios swift tableview collectionview


【解决方案1】:

尝试在 else 部分使用保护:

 guard self.reccomend?.count > 0 else{
       return
    }
 self.collectionView.reloadData()

【讨论】:

    【解决方案2】:

    您只需要检查一下,如果有一些数据,请致电self.collectionView.reloadData()。像这样:

    if (self.reccomend.count>0)
    {
       self.collectionView.reloadData()
    }
    

    【讨论】:

      【解决方案3】:

      您的应用程序正在崩溃,因为您强制解包:(self.reccomend?.count)!

      这里的属性reccomend 是可选的,这意味着'reccomend' 可能为nil。在您的情况下,reccomend 为 nil,因为当您添加 ! 时,您告诉我们您知道它永远不会为 nil,如果是,应用程序将崩溃,因此它会崩溃。

      if let recommended = self.reccomend {
        if recommended.count < 1 {
          self.tableView.tableHeaderView = nil
        } else {
          self.collectionView.reloadData()
        }
      } else {
        //if self.reccomend is nil, do something
      }
      

      但这不是你的主要问题 - 你的主要问题是 self.reccomend 设置为等于 data 后为零

      您需要检查self.reccomend 是什么类型的数组以及data 是什么类型的数组,以确保它们是相同的类型并且您得到了您期望的JSON 响应。所以设置断点并弄清楚这一点,因为这就是你的强制解包失败的原因,也是你永远不应该强制解包任何类型的互联网响应的原因。

      【讨论】:

      • self.reccomend 和数据是同一类型。我从外部 api 获取 JSON 数据。我怎样才能复制同样的问题?
      • 可以在设置self.reccomend 之前打印data 等于它吗?您还可以显示您将self.reccomend 声明为属性的位置吗?将两者都添加到您上面的问题中。 self.reccomend 是声明为 var reccomend = [[String: AnyObject]]? 还是别的什么?
      猜你喜欢
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多