【问题标题】:Swift 3 - UICollectionview Problems on iOS 11.2Swift 3 - iOS 11.2 上的 UICollectionview 问题
【发布时间】:2023-03-10 18:11:01
【问题描述】:

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 的初学者。我感谢您的所有回复。谢谢!

注意:我简化了代码,因为实际代码非常复杂。

【问题讨论】:

  • 你试过简化代码,并确认它显示同样的问题?
  • 应用委托出现什么错误,有没有崩溃
  • @MilanNosáľ 哦,对不起,我的意思是我已经删除了所有不重要的代码来显示
  • @HarshalBhavsar 它说线程 1:信号 SIGABRT

标签: ios swift xcode uicollectionview collectionview


【解决方案1】:

我用collectionView.reloadSections(IndexSet(integer: 0)) 替换collectionView.reloadData() 解决了我的问题1。我从UITableview Scrolls to Top on Reload得到了参考

通过删除collectionView.reloadData()解决了问题2

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2016-12-29
    相关资源
    最近更新 更多