Лучшее практическое решение
Создание модели ответа и модели комментариев, содержащей список объектов ответов
class Comment {
var commentId: Int
var commentText: String
var replies: [Reply]
init(commentId: Int, commentText: String, replies: [Reply]) {
self.commentId = commentId
self.commentText = commentText
self.replies = replies
}
}
class Reply {
var replyId: Int
var replyText: String
init(replyId: Int, replyText: String) {
self.replyId = replyId
self.replyText = replyText
}
}
Создание UICollectionReusableView для заголовка комментария
class CommentHeader: UICollectionReusableView {
@IBOutlet weak var commentTextLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func configure(with comment: Comment) {
commentTextLabel.text = comment.commentText
}
}
Создать UICollectionViewCell для ответа
class ReplyCell: UICollectionViewCell {
@IBOutlet weak var replyTextLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func configure(with reply: Reply) {
replyTextLabel.text = reply.replyText
}
}
Создание класса CommentsViewController, который имеет UICollectionView и список данных комментариев
Внимание, заголовок и ячейка зарегистрированы в представлении коллекции в методе viewDidLoad
class CommentsViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
var comments: [Comment] = [Comment]()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.register(UINib(nibName: "CommentHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CommentHeaderIdentifier")
collectionView.register(UINib(nibName: "ReplyCell", bundle: nil), forCellWithReuseIdentifier: "ReplyCellIdentifier")
comments = getDummyComments(with: 3)
}
func getDummyComments(with count: Int) -> [Comment] {
var comments = [Comment]()
for i in 1...count {
comments.append(Comment(commentId: i, commentText: "Comment \(i)", replies: getDummyReplies(with: i)))
}
return comments
}
func getDummyReplies(with count: Int) -> [Reply] {
var replies = [Reply]()
for i in 1...count {
replies.append(Reply(replyId: i, replyText: "Reply \(i)"))
}
return replies
}
}
И, наконец, установите источник данных UICollectionView и методы делегата
extension CommentsViewController: UICollectionViewDataSource {
// for cell
func numberOfSections(in collectionView: UICollectionView) -> Int {
return comments.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return comments[section].replies.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let replyCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReplyCellIdentifier", for: indexPath) as! ReplyCell
replyCell.configure(with: comments[indexPath.section].replies[indexPath.row])
return replyCell
}
// for header
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let commentHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CommentHeaderIdentifier", for: indexPath) as! CommentHeader
commentHeader.configure(with: comments[indexPath.section])
return commentHeader
}
return UICollectionReusableView()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100) // this height is used for the example, you can use self sizing for height
}
}
extension CommentsViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100) // this height is used for the example, you can use self sizing for height
}
}
Давайте посмотрим на экран комментариев:)