簡體   English   中英

Swift 3-iOS 11.2上的UICollectionview問題

[英]Swift 3 - UICollectionview Problems on iOS 11.2

我有兩個關於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不直接支持iOS11。但是現在在Xcode 9.2 iOS 11.2模擬器上,我無法清晰地滾動collectionview,然后在UIAppDelegate使用Thread 1: signal SIGABRT.中斷了它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的初學者。 感謝您的所有答復。 謝謝!

注意:我將代碼簡化了,因為實際的代碼確實很復雜。

我通過用collectionView.reloadSections(IndexSet(integer: 0))替換collectionView.reloadData()解決了問題1。 我從UITableview中獲得了參考

通過刪除collectionView.reloadData()解決了問題2

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM