[英]Swift 3 - UICollectionview Problems on iOS 11.2
I have two problems with UICollectionView
. 我有两个关于UICollectionView
问题。 Following are the scenarios: 以下是方案:
Problem 1 问题1
I have an app that is created in Xcode 8. It's build on iOS 10.3 simulator and everything was good. 我有一个用Xcode 8创建的应用程序。它是在iOS 10.3模拟器上构建的,一切都很好。 But when I tried on my device with iOS 11.2, I have a problem with the UICollectionView
. 但是,当我在装有iOS 11.2的设备上尝试时, UICollectionView
出现了问题。 When a new data comes, it should be appears on bottom (that's what I want and have done on iOS 10.3). 当有新数据出现时,它应该显示在底部(这就是我想要的,并且在iOS 10.3上已经完成)。 But iOS 11.2 makes it to scroll to top when the new data comes on bottom. 但是,当新数据排在底部时,iOS 11.2使其滚动到顶部。
Problem 2 问题2
Then I have migrated from Xcode 8 to Xcode 9.2 because Xcode 8 has no direct support to iOS 11. But now on Xcode 9.2 iOS 11.2 simulator, I can't scroll the collectionview clearly and then it breaks in UIAppDelegate
with Thread 1: signal SIGABRT.
然后我从Xcode 8迁移到Xcode 9.2,因为Xcode 8不直接支持iOS11。但是现在在Xcode 9.2 iOS 11.2模拟器上,我无法清晰地滚动collectionview,然后在UIAppDelegate
使用Thread 1: signal SIGABRT.
中断了它Thread 1: signal SIGABRT.
As long as I know, this kind of error will appear whenever I have a disconnected outlet. 据我所知,每当我的插座断开连接时,都会出现这种错误。 But I'm pretty sure all of my outlets have connected. 但是我很确定我所有的插座都已连接。
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
}
Can anyone help me to solve this? 谁能帮我解决这个问题? I am just a beginner on Swift by the way. 顺便说一下,我只是Swift的初学者。 I appreciate all of your responses. 感谢您的所有答复。 Thanks! 谢谢!
NB: I've simplicize the code because the actual code is really complicated. 注意:我将代码简化了,因为实际的代码确实很复杂。
I solved my problem 1 with replacing collectionView.reloadData()
with collectionView.reloadSections(IndexSet(integer: 0))
. 我通过用collectionView.reloadSections(IndexSet(integer: 0))
替换collectionView.reloadData()
解决了问题1。 I got reference from UITableview Scrolls to Top on Reload 我从UITableview中获得了参考
Problem 2 solved by removing collectionView.reloadData()
通过删除collectionView.reloadData()
解决了问题2
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.