1

我试图在 UICollectionView 中显示数据,它的工作方式与我在整个应用程序中所做的完全一样,但由于某种原因,它在这种情况下不起作用:

这是理解问题所需的代码:

一些控制器:

func showContactsView(sender: RoundButton) -> Void {
        DispatchQueue.main.async {
            self.someView = SomeView()
            self.view.addSubview(self.someView)
            self.someView.setSomeViewViewConstraints(container: 
    self.view)
            self.someView.alpha = 0.0
            self.someView.contactsCollectionView.delegate = self
            self.someView.contactsCollectionView.dataSource = self
            self.someView.tabControlCallbacks["contacts"] = self.searchContacts
        }
    }

控制器初始化包含 UICollectionView 的视图。这部分由自动布局和约束处理,我没有问题。它还继承自 UIColleCtionViewDelegate 和 UICollectionViewDataSource。:

class SomeController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate

视图初始化 collectionView 并由 SegmentedControl 显示,它完美地工作:

private func initCollectionViews() {
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 50, height: 50)

        contactsCollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        contactsCollectionView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        contactsCollectionView.showsVerticalScrollIndicator = false
        contactsCollectionView.showsHorizontalScrollIndicator = false
        contactsCollectionView.register(AModelCollectionViewCell.self, forCellWithReuseIdentifier: "ContactsSelectModelCell")
        contactsCollectionView.translatesAutoresizingMaskIntoConstraints = false


        // Constraints to add when view is set dynamically
        contactsCollectionViewConstraints["leading"] = NSLayoutConstraint(item: contactsCollectionView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 0.0)
        contactsCollectionViewConstraints["trailing"] = NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: contactsCollectionView, attribute: .trailing, multiplier: 1.0, constant: 0.0)
        contactsCollectionViewConstraints["top"] = NSLayoutConstraint(item: tabControl, attribute: .bottom, relatedBy: .equal, toItem: contactsCollectionView, attribute: .top, multiplier: 1.0, constant: 0.0)
        contactsCollectionViewConstraints["bottom"] = NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: contactsCollectionView, attribute: .bottom, multiplier: 1.0, constant: 0.0)
    }

并且分段控件显示的值发生了变化:

func cleanViews() {
        if self.contactsCollectionView.superview != nil {
            self.contactsCollectionView.removeFromSuperview()
        }
        if self.favoritesCollectionView.superview != nil {
            self.favoritesCollectionView.removeFromSuperview()
        }
    }

func bounce() {
        self.cleanViews()
        switch self.actualControl {
        case -1:
            break
        case 0:
            // Some code
        case 1:
            self.addSubview(self.contactsCollectionView)
            for constraint in self.contactsCollectionViewConstraints {
                self.addConstraint(constraint.value)
            }
            if let callback = self.tabControlCallbacks["contacts"]! {
                callback()
            }
        default:
            fatalError("Unknown control")
        }
    }

    // Selectors UISegmentedControl
    func valueChanged(sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == actualControl {
            tabControl.isMomentary = true
            sender.selectedSegmentIndex = -1
            tabControl.isMomentary = false
        }
        actualControl = sender.selectedSegmentIndex


        DispatchQueue.main.async {
            self.bounce()
        }
    }

如您所见,我正在设置从控制器到视图的回调,以便对我的网络执行操作并获取要在 collectionView 上显示的数据。以下是控制器中的方法:

  //Collection Views Actions/Callbacks for selectContactsView
    func searchContacts() -> Void {
        let params : [String:Any] = [
            "id" : "-1",
            "name_begin" : ""
        ]

        self.network?.get(......, callback: afterSearchContacts)
    }

    func afterSearchContacts() -> Void {

        DispatchQueue.main.async {

            self.contactsSearchResults.removeAll()
            let usersData : [[String:Any]] = self.network?.lastResponse!["users"] as! [[String:Any]]
            let groupsData : [[String:Any]] = self.network?.lastResponse!["groups"] as! [[String:Any]]

            for userData in usersData {
                self.contactsSearchResults.append(User(fromData: userData))
            }
            for groupData in groupsData {
                self.contactsSearchResults.append(Group(fromData: groupData))
            }
            self.contactsView.contactsCollectionView.reloadData()
        }
    }

reloadData 不起作用。在解释哪些部分有效,哪些无效之前,这里是代表和数据源的方法:

//MARK: - UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


        switch self.contactsView.actualControl {
        case 1:

            if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContactsSelectModelCell", for: indexPath) as? AModelCollectionViewCell {
                cell.model = contactsSearchResults[indexPath.item]
                cell.fillCell()
                return cell
            }
        case 0:
                //some Code
                return cell
            }
        default:
            print("Unknown cell type")
        }
        fatalError("Unknown cell type")
    }

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

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = collectionView == contactsView.contactsCollectionView ? contactsSearchResults.count : favoritesSearchResults.count

        return count
    }

    //MARK: - UICollectionViewFlowLayoutDelegate
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 50.0, height: 50.0)
    }

我有一个自定义集合视图单元格,它正在处理其他集合视图,所以没问题。当我进行注册时,我还负责区分所有不同 CollectionViews 的标识符。在这种情况下,当我使用断点时,部分中的项目数> 0,但是当我在网络设置 contactsSearchResult 后在主线程中执行 reloadData 时,它永远不会通过 collectionView cellForItemAt。

任何的想法?

4

1 回答 1

-1

对于那些阅读这篇文章的人,我最终编写了自己的 CollectionView。像魅力一样工作。

于 2017-07-10T12:11:48.647 回答