- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
以下函数加载一个 Messages
数组,重新加载 collectionView
并(应该)滚动到最后一项。
问题是最后一个项目向上滚动太远超出 View ,因此不可见。
注意:我使用了 3 种不同的“原型(prototype)”自动调整大小的单元格,它们根据消息的类型(文本消息、图像消息等)出列。
这似乎是一项如此简单的任务,但我开始为这个任务费尽心思。任何帮助都会很棒:)
func loadConversation(){
DataService.run.observeUsersMessagesFor(forUserId: chatPartnerId!) { (chatLog) in
self.messages = chatLog
self.collectionView.reloadData()
if self.messages.count > 0 {
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .bottom , animated: true)
}
}
}//end func
编辑:numberOfItemsInSection 函数
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
编辑:cellForItemAt
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let message = messages[indexPath.item]
let uid = Auth.auth().currentUser?.uid
if message.fromId == uid {
if message.imageUrl != nil {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ConversationCellImage", for: indexPath) as! ConversationCellImage
cell.configureCell(message: message)
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ConversationCellSender", for: indexPath) as! ConversationCellSender
cell.configureCell(message: message)
return cell
}//end if message.imageUrl != nil
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ConversationCell", for: indexPath) as! ConversationCell
cell.configureCell(message: message)
return cell
}//end if
}//end func
编辑:ConversationCell
Class
自动调整单元格的高度。
class ConversationCell: UICollectionViewCell {
@IBOutlet weak var chatPartnerProfileImg: CircleImage!
@IBOutlet weak var messageLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
chatPartnerProfileImg.isHidden = false
}//end func
func configureCell(message: Message){
messageLbl.text = message.message
let partnerId = message.chatPartnerId()
DataService.run.getUserInfo(forUserId: partnerId!) { (user) in
let url = URL(string: user.profilePictureURL)
self.chatPartnerProfileImg.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "placeholder"), options: [.continueInBackground, .progressiveDownload], completed: nil)
}//end getUserInfo
}//end func
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = 10.0
self.layer.shadowRadius = 5.0
self.layer.shadowOpacity = 0.3
self.layer.shadowOffset = CGSize(width: 5.0, height: 10.0)
self.clipsToBounds = false
}//end func
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
//toggles auto-layout
setNeedsLayout()
layoutIfNeeded()
//Tries to fit contentView to the target size in layoutAttributes
let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
//Update layoutAttributes with height that was just calculated
var frame = layoutAttributes.frame
frame.size.height = ceil(size.height) + 18
layoutAttributes.frame = frame
return layoutAttributes
}
}//end class
最佳答案
您应该在主线程上执行与 UI 相关的任务。按照以下方式更新您的函数 loadConversation
func loadConversation() {
DataService.run.observeUsersMessagesFor(forUserId: chatPartnerId!) { (chatLog) in
self.messages = chatLog
DispatchQueue.main.async {
self.collectionView.reloadData()
self.collectionView.layoutIfNeeded()
if self.messages.count > 0 {
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
// set animated false/true, based on the new messages. if many new messages keep it false
self.collectionView.scrollToItem(at: indexPath, at: .bottom , animated: true)
}
}
}
}
编辑
闪烁是由于设置了 userImage。它应该在主线程上。更新为
DispatchQueue.main.async {
self.chatPartnerProfileImg.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "placeholder"), options: [.continueInBackground, .progressiveDownload], completed: nil)
}
尝试并分享结果。
关于ios - 为什么 collectionView.scrollToItem 使用 Swift 将 indexPath 滚动到 View 之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51760393/
最近想根据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
我是一名优秀的程序员,十分优秀!