【问题标题】:Collectionview with coredata problems有 coredata 问题的 Collectionview
【发布时间】:2022-02-15 12:55:34
【问题描述】:

问题是当我在页面(TabNavigation)之间切换并返回此页面时,意外添加了一个单元格,我将代码重写了很多次,有人可以帮助我吗?

CoreData 实现了它以将收藏夹保存在此集合视图中,除了这个小错误之外一切正常

                            var Distance : String!
                            var Logo : UIImage!
                            var pp : String!
                            var menuu : UIButton!
                            var loc : String!
                            var shop: [NSManagedObject] = []
                          
                           
                     
                    @IBOutlet weak var ShopCollectionView: UICollectionView!

覆盖 func viewDidLoad() { super.viewDidLoad()

        ShopCollectionView.layer.cornerRadius = 10
        ShopCollectionView.layer.masksToBounds = true
    
        // register cell
      let nibCell = UINib(nibName: ShopCollectionViewCellId, bundle: nil)
        ShopCollectionView.register(nibCell, forCellWithReuseIdentifier: ShopCollectionViewCellId)
        ShopCollectionView.delegate = self
        ShopCollectionView.dataSource = self
    

            let center = UNUserNotificationCenter.current()
                center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in }

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    
    let managedContext = appDelegate.persistentContainer.viewContext
    
    let entity = NSEntityDescription.entity(forEntityName: "ShopsData", in: managedContext)!
    
    let item = NSManagedObject(entity: entity, insertInto: managedContext)
    
    let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "ShopsData")
    
    do {
        let result = try? managedContext.fetch(fetch) as? [ShopsData]
        shop = result ?? []
    } catch {
        fatalError()
    }
    
    collectionView.reloadData()
}
       



        override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        
           return shop.count + 1
            
        }

<This is my writed method>

        override  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
           if indexPath.row >= shop.count {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ShopCollectionViewCellId, for: indexPath) as! ShopCollectionViewCell
     
        return cell
    } else {

        
        let shop = shop[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ShopCollectionViewCellId, for: indexPath) as! ShopCollectionViewCell
      
        cell.People.text = shop.value(forKey: "actualCustomers") as? String
        cell.Location.text = shop.value(forKey: "location") as? String
         
          return cell 
        

           }         

       }
    
    

这是我写的代码

【问题讨论】:

    标签: swift core-data uicollectionview uikit uicollectionviewcell


    【解决方案1】:

    在您的numberOfItemsInSection 中,您返回shop.count + 1,这意味着您告诉您的集合视图向您显示比您拥有的实际数据多1 个单元格。

    然后在您的 cellForItemAt indexPath 中,您通过创建 1 个空白单元格来处理此问题。

    如果建议您对这些功能进行如下更改,如下所示,也许您会看到您期望的结果

    override func collectionView(_ collectionView: UICollectionView,
                                 numberOfItemsInSection section: Int) -> Int
    {
        return shop.count
    }
    
    override func collectionView(_ collectionView: UICollectionView,
                                 cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let shop = shop[indexPath.row]
        let cell =
            collectionView.dequeueReusableCell(withReuseIdentifier: ShopCollectionViewCellId,
                                                 for: indexPath) as! ShopCollectionViewCell
        
        cell.People.text = shop.value(forKey: "actualCustomers") as? String
        cell.Location.text = shop.value(forKey: "location") as? String
        
        return cell
    }
    

    【讨论】:

    • 嗨,弗兰克,谢谢您的回答!我编辑了代码,但我遇到了同样的问题。
    • @Alex_popa_31 你在使用获取结果控制器吗?有什么方法可以在我可以运行和调试的 repo 上创建一个测试项目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2015-09-04
    • 2012-10-26
    相关资源
    最近更新 更多