초기 테이블로드 후 모든 것이 올바르게 작동합니다. 그러나 새 셀을 삽입하고 빠르게 스크롤하면 크기가 다시 계산되는 일부 셀 (애니메이션)을 볼 수 있습니다. 정말 이상하고 2-3 셀 최대가 발생합니다. 나는 반전 된 셀과 사용자 정의 흐름 레이아웃을 가진 상향식 콜렉션 뷰를 사용하고 있지만 애니메이션도 없습니다. 또한, 키보드 가죽 스크롤, 그래서스크롤하는 동안 UICollectionView 셀의 크기가 재 계산됩니다 (깜박임)
셀 삽입 .. 함께 할 수있는 뭔가가있을 때 문제의
self.collectionView?.insertItems(at: [indexPath])
UIView.performWithoutAnimation {
self.collectionView?.reloadItems(at: indexPaths)
self.view.layoutIfNeeded()
}
self.collectionView?.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
비디오 : 또한
http://vimple.co/3c39cb325b9c4ec19173fea015b6cc8b
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let newIndex : Int = messagesDataArray.count - 1 - indexPath.item
if messagesDataArray[newIndex].otherPerson == true {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ConversationCellOtherPerson", for: indexPath) as! ConversationCellOtherPerson
cell.data = messagesDataArray[newIndex]
cell.personImageView.image = otherPersonImage
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ConversationCellUser", for: indexPath) as! ConversationCellUser
cell.data = messagesDataArray[newIndex]
return cell
}
}
. ConversationCellUser :
class ConversationCellUser : UICollectionViewCell {
let messageLabel = ConversationLabel()
let mainContainerView = UIView()
let textContainerView : UIView = {
let view = UIView()
view.layer.cornerRadius = 20
return view
}()
var data : MessagesInfo? { didSet { updateCell() } }
func updateCell() {
guard let data = data else { return }
messageLabel.text = data.messageText
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
clipsToBounds = true
messageLabel.translatesAutoresizingMaskIntoConstraints = false
textContainerView.translatesAutoresizingMaskIntoConstraints = false
mainContainerView.translatesAutoresizingMaskIntoConstraints = false
mainContainerView.addSubview(textContainerView)
textContainerView.addSubview(messageLabel)
contentView.addSubview(mainContainerView)
NSLayoutConstraint(item: textContainerView, attribute: .right, relatedBy: .equal, toItem: mainContainerView, attribute: .right, multiplier: 1, constant: -10).isActive = true
textContainerView.addConstraintsWithFormat(format: "H:|-14-[v0]-14-|", views: messageLabel)
textContainerView.addConstraintsWithFormat(format: "V:|-10-[v0]-10-|", views: messageLabel)
contentView.addConstraintsWithFormat(format: "H:|[v0]|", views: mainContainerView)
contentView.addConstraintsWithFormat(format: "V:|[v0]|", views: mainContainerView)
textContainerView.backgroundColor = .lightGray
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
super.apply(layoutAttributes)
transform = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: 0)
updateConstraints()
setNeedsUpdateConstraints()
}
}
ConversationLabel :
class ConversationLabel : BaseMessageLabel {
override init(frame: CGRect) {
super.init(frame: frame)
self.textColor = .white
self.font = UIFont(name: "AvenirNext-Medium", size: 14)
self.lineBreakMode = .byWordWrapping
self.numberOfLines = 0
self.preferredMaxLayoutWidth = 200
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
SizeForItem :
Github - jochenschoellig - ChatCollectionViewFlowLayout
:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var height : CGFloat = 80
let text = messagesDataArray[indexPath.row].messageText
height = estimateFrameFor(text).height + 20
return CGSize(width: collectionView.bounds.size.width, height: height)
}
private func estimateFrameFor(_ text : String) -> CGRect {
let size = CGSize(width: 200, height: 1000)
return NSString(string: text).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName : UIFont(name: "AvenirNext-Medium", size: 14)!], context: nil)
}
그리고 내가 사용하고있어 FlowLayout의이입니다 안녕하세요, 문제가있는 작은 프로젝트를 만들었습니다. Github - testLP
귀하의 cellForRowAtIndexPath에 대한 코드를 공유하십시오. – KKRocks