- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用这种方法来滚动我的收藏 View 。
import UIKit
extension UICollectionView {
func scrollToLast() {
guard numberOfSections > 0 else {
print("number of sections < 0")
return
}
let lastSection = numberOfSections - 1
guard numberOfItems(inSection: lastSection) > 0 else {
print("number of items < 0")
return
}
let lastItemIndexPath = IndexPath(item: numberOfItems(inSection: lastSection) - 1,
section: lastSection)
self.scrollToItem(at: lastItemIndexPath, at: .centeredVertically, animated: true)
print("last Item (section, item #): \(lastItemIndexPath)")
}
}
2019-11-04 06:01:13.017384-0800 AppName[17598:4494554] [CollectionView] An attempt to prepare a layout while a prepareLayout call was already in progress (i.e. reentrant call) has been ignored. Please file a bug. UICollectionView instance is (; layer = ; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: ; dataSource: appBundle.instance
DataService.instance.messageListener(roomId: room.id, { (result) in
self.messageArr.append(result)
self.messageArr = self.messageArr.sorted(by: { $0.date < $1.date})
self.collection.reloadData()
self.collection.scrollToLast()
self.messageTextArea.text = ""
})
scrollToLast()
时方法在其他地方, View 滚动到最后一项应该 - 但只有在 View 手动滚动到底部之后。我也尝试将代码移至
viewDidAppear
根据下面的答案,但没有效果。
最佳答案
这与您的问题无关,但我有一些建议:
1)调用self.scrollToItem之前不需要调用DispatchQueue.main.async,因为在DataService.instance.messageListener中已经切换到主线程。
2) messageArr 存在潜在的多线程问题,因为您从 DataService messageListener completionHandlers 线程中编辑了它(它可能在线程上)。据我了解 messageArr 是您的 Collection View 的数据源。因此,当您滚动 Collection View 或重新加载 Collection View 时,collectionView 将从主线程访问 messageArr。这将导致崩溃或未定义的行为,因为您从两个线程访问 messageArr 数组而没有锁定。解决方案之一是将整个 DataService.instance.messageListener 完成处理程序异步到主队列。
DataService.instance.messageListener(roomId: room.id, { (result) in
DispatchQueue.main.async {
self.messageArr.append(result)
self.messageArr = self.messageArr.sorted(by: { $0.date < $1.date })
self.collection.reloadData()
self.collection.scrollToLast()
self.messageTextArea.text = ""
}
})
关于ios - UICollectionView 不会滚动到项目(在 : IndexPath, 处:位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58695078/
最近想根据indexPaths删除单元格,所以函数的入参是[IndexPath]类型,需要根据把[IndexPath]拆分成几个数组indexPath.section,有什么简单的方法吗?例如 ind
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我有一个 TableViewController,其中包含使用 CoreData 的数据条目列表。我还有带 TextView 的 ViewController。我已经完成了对项目的添加、保存和删除等操
我正在编写一个 UICollectionViewLayout 子类。 我知道 Collection View 有多少个部分...numberOfSections。 我知道每个部分有多少项目...num
这是我的 collectionViewCell 图像。 我在 tableViewCell 中有 collectionView 并且在 collectionViewCell 中有一个按钮,它完全符合我的
这个问题在这里已经有了答案: Passing data between view controllers (45 个答案) 关闭 3 年前。 我在 UIViewController 中定义它并尝试在
我在我正在开发的应用程序中的表格单元格上设置长按手势时遇到了一些问题。我已经尝试了三种不同的配置,但还没有一种能够成功运行。 场景 1) 我将 UILongPressGestureRecognizer
在我更新到 Xcode 7.2 后,弹出一个错误提示“下标的使用不明确”: func tableView(tableView: UITableView, cellForRowAtIndexPath i
我在 UITaleViewCell 中有一个 UIImageView,我向 UIImageView 添加了一个点击识别器。 - (IBAction)ui_tapped:(UITapGestureRec
我正在尝试追踪应用程序中的严重崩溃。 我有一些代码可以有效地做到这一点: if let indexPath = self.tableView.indexPath(for: myTableViewCel
我有一个 UITableView,一个自定义单元格的自定义类和我的 ViewController swift: private var model_firma = [Firme]() var firm
我有一个 Tableview,显示一个包含 7 行的部分,我想要做的是,当我按第 0 行时,我将其发送到另一个 View ,表中的所有信息都是通过使用修复的代码加载的,我的问题是如何在“prepare
我注意到要禁用表格单元格的突出显示功能,您可以使用以下方法: override func tableView(tableView: UITableView, didSelectRowAtIndexPa
这个问题在这里已经有了答案: Issue Detecting Button cellForRowAt (3 个答案) 关闭 2 年前。 使用此代码,我只获得具有 indexpath.row 如何从中
我正在开发一个待办事项应用程序。在我的应用程序中,我按下复选框按钮来删除一行。我编写这段代码是为了将 indexPath.row 传递到我的复选框按钮中: cell.checkbox.tag = in
我有一个像这样的UIViewController: class ViewController { override func viewDidLoad() { super.vie
不知道你有没有遇到过这种情况,我用的是该函数 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPa
UITableViewController 的 numberOfSection 和 numberOfRowsInSection 被调用,但 cellForRow 没有。可能是什么原因 ?下面是 tab
我是 Stack Overflow 的新手,所以请保持温柔! 我正在设计一个应用程序,它从 firebase 检索对象并在 firebase 中显示它们。 我在这里配置firebase: func a
我手头有一个非常原始的问题,那就是当我将点击手势识别器添加到我的自定义单元格时,我无法调用 didSelectRowAt 方法。 我的代码如下: import UIKit class Language
我是一名优秀的程序员,十分优秀!