ios - 如何组织表格视图单元格
问题描述
我在一个表格视图中有两种不同类型的表格视图单元格。第一个单元格打印出对帖子的原始评论,第二个单元格打印出对另一条评论的评论。目前,tableview 不按特定顺序打印出所有正确的单元格。但是,我想按特定顺序打印单元格。我希望包含对另一个评论的评论的单元格出现在它被评论的评论下方。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
let cell = tableView.dequeueReusableCell(withIdentifier: "Main", for: indexPath) as! PostTableViewCell
//Configure the cell
cell.PostView.layer.cornerRadius = 5
cell.PostView.layer.masksToBounds = false
cell.PostView.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
cell.PostView.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.PostView.layer.shadowOpacity = 0.9
let post = Comments[indexPath.row] as! [String: AnyObject]
let commentname = post["author"] as? String
sendAuthor = post["author"] as? String
cell.CommentersName.setTitle(commentname, for: .normal)
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
let formating = timeStampDate as Date
cell.CommentTime.text = dateFormatter.string(from: formating)
}
cell.comment.text = post["content"] as? String
textViewDidChange(cell.comment)
cell.comment.frame.size.width = 344
cell.comment.sizeToFit()
cell.comment.clipsToBounds = true
cell.REply.frame.origin.y = cell.comment.frame.maxY + 10
cell.report.frame.origin.y = cell.comment.frame.maxY + 10
cell.Likes.frame.origin.y = cell.comment.frame.maxY + 10
cell.LikesNumber.frame.origin.y = cell.comment.frame.maxY + 10
cell.PostView.frame.size.height = cell.comment.frame.maxY + 50
TableView.rowHeight = cell.PostView.frame.size.height + 20
cell.CommentersName.sizeToFit()
cell.pole.frame.origin.x = cell.CommentersName.frame.maxX + 5
cell.CommentTime.frame.origin.x = cell.pole.frame.maxX + 5
let numLikes = post["num_likes"] as? NSNumber
cell.LikesNumber.text = String(describing: numLikes!)
replyId = post["id"] as? String
let replyTo = post["reply_to"] as? String
let postID = post["post_id"] as? String
if replyTo == postID {
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Reply", for: indexPath) as! RepliesTableViewCell
cell.ReplyCustomCell.layer.cornerRadius = 5
cell.ReplyCustomCell.layer.masksToBounds = false
cell.ReplyCustomCell.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
cell.ReplyCustomCell.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.ReplyCustomCell.layer.shadowOpacity = 0.9
let post = Comments[indexPath.row] as! [String: AnyObject]
cell.ReplyText.text = post["content"] as? String
let commentname = post["author"] as? String
cell.author.setTitle(commentname, for: .normal)
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
let formating = timeStampDate as Date
cell.time.text = dateFormatter.string(from: formating)
}
let numLikes = post["num_likes"] as? NSNumber
cell.num_likes.text = String(describing: numLikes!)
textViewDidChange(cell.ReplyText)
cell.ReplyText.frame.size.width = 232
cell.ReplyText.sizeToFit()
cell.ReplyText.clipsToBounds = true
cell.author.sizeToFit()
cell.pole.frame.origin.x = cell.author.frame.maxX + 5
cell.time.frame.origin.x = cell.pole.frame.maxX + 5
cell.Likes.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.num_likes.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.reportButton.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.replyButton.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.ReplyCustomCell.frame.size.height = cell.ReplyText.frame.maxY + 50
TableView.rowHeight = cell.ReplyCustomCell.frame.size.height + 20
return cell
}
cell.checkfornightmode()
return cell
}
相互关联的评论具有相同的“id”,我将如何组织单元格,以便主评论的评论将列在原始评论下。谢谢
解决方案
您可以创建一个 Comment 自定义对象类,该类将包含一组子评论和主要评论,以正确安排或管理您的数据结构。之后,您可以在表格视图单元格中正确使用它。好的,例如,您可以拥有以下数据结构。
创建一个评论类:
class Comment {
comment_id
content
post_id
reply_to
}
现在为您的表格视图再创建一个类:
class CommentTableDataModel {
var mainComment: Comment // Of type Comment class
var replies: [Comment] // Array of type Comment class for sub comments
}
因此,现在只需遍历您的 firebase Comments 数组并准备一个类型为“CommentTableDataModel”对象的数组列表作为表的数据源。所以最后你将有一个类型为“CommentTableDataModel”的对象数组,每个“CommentTableDataModel”类型的对象都包含主要的评论信息以及回复信息列表,这样你就可以管理你的数据。
推荐阅读
- django - 找不到参数'('','','','')'的反向。1 种模式尝试错误
- java - 如何通过 Sonarqube 异常测试覆盖率
- javascript - CanvasJS - 从用户输入渲染时间线图
- base64 - 如何在 NodeJs 中使用 express-swagger-generator npm 在 base64 中编码 swagger 请求
- python - 解析字符串类型的函数并返回一个值为 int 和 str 的元组
- javascript - localStorage 第二个参数不接受 input.value
- c# - 如何在更新查询中使用组合框文本
- java - 在 For 循环中从 LinkedList 中删除元素;备择方案?
- c# - GoDaddy API - 域名单价值
- python - Python中意外的小数位数