代码之家  ›  专栏  ›  技术社区  ›  catcatcat

Swift 3-iOS 11.2上的UICollectionview问题

  •  0
  • catcatcat  · 技术社区  · 8 年前

    我有两个问题 UICollectionView . 以下是场景:

    问题1

    我有一个在Xcode 8中创建的应用程序。它基于iOS 10.3模拟器,一切都很好。但当我在iOS 11.2设备上试用时,我发现 UICollectionView . 当一个新的数据出现时,它应该出现在底部(这是我想要的,也是我在iOS 10.3上所做的)。但iOS 11.2允许在新数据位于底部时滚动到顶部。

    问题2

    然后我从Xcode 8迁移到了Xcode 9.2,因为Xcode 8不直接支持iOS 11。但现在在Xcode 9.2 iOS 11.2模拟器上,我无法清晰地滚动collectionview,然后它就出现了 UIAppDelegate 具有 Thread 1: signal SIGABRT.

    只要我知道,每当我断开插座时,就会出现这种错误。但我很确定我所有的渠道都有联系。

    import UIKit
    
    class cvViewController: UIViewController, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegate {
    
    @IBOutlet var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView.delegate = self
    
        collectionView.register(UINib.init(nibName: "Cell", bundle: nil), forCellWithReuseIdentifier: "Cell")
        if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
        }
    
        loadAPI(Page: 1) //display data
    
        scrollDownButton.clipsToBounds = true
        scrollDownButton.layer.cornerRadius = 15
    
        sendButton.addTarget(self, action: #selector(cvViewController.sendMessage), for: .touchDown) //push a new data
    
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        scheduledTimerWithTimeInterval()
        if move == 0{
            scrollToBottomAnimated(animated: true)
        }
    
        scrollDownButton.addTarget(self, action: #selector(cvchatViewController.scrollToBottomAnimated), for: .touchDown) //it will display last item on screen
    
        flowLayout.scrollDirection = .vertical
    
    }
    
    func scrollToBottomAnimated(animated: Bool) {
        guard self.collectionView.numberOfSections > 0 else{
            return
        }
    
        let items = self.collectionView.numberOfItems(inSection: 0)
        if items == 0 { return }
    
        let collectionViewContentHeight = self.collectionView.collectionViewLayout.collectionViewContentSize.height
        let isContentTooSmall: Bool = (collectionViewContentHeight < self.collectionView.bounds.size.height)
    
        if isContentTooSmall {
            self.collectionView.scrollRectToVisible(CGRect(x: 0, y: collectionViewContentHeight - 1, width: 1, height: 1), animated: animated)
            return
        }
    
        self.collectionView.scrollToItem(at: NSIndexPath(item: items - 1, section: 0) as IndexPath, at: .bottom, animated: animated)
    }
    
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return chatPages.count
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
    
         cell.vNameLabel.text = chatPages[indexPath.row].username
         cell.VCDateLabel.text = chatPages[indexPath.row].timeSent
         cell.vImageSend.image = UIImage(named: "1x1px")
    
        return cell
    }
    
    
    func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    

    有人能帮我解决这个问题吗?顺便说一下,我只是Swift的初学者。我很感激你的回答。谢谢

    NB:我已经简化了代码,因为实际的代码非常复杂。

    1 回复  |  直到 8 年前
        1
  •  0
  •   catcatcat    8 年前

    我用替换解决了问题1 collectionView.reloadData() 具有 collectionView.reloadSections(IndexSet(integer: 0)) . 我从 UITableview Scrolls to Top on Reload

    问题2通过删除解决 collectionView。重载数据()

    谢谢