【发布时间】:2018-07-16 12:03:07
【问题描述】:
我在一个表格视图中有两种不同类型的表格视图单元格。第一个单元格将原始 cmets 打印到帖子中,第二个单元格将 cmets 打印到另一个评论。目前,tableview 不按特定顺序打印出所有正确的单元格。但是,我想按特定顺序打印单元格。我希望包含 cmets 到另一个评论的单元格出现在它被评论的评论下方。
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
}
相互关联的 cmets 具有相同的“id”,我将如何组织单元格,以便主评论的 cmets 将列在原始评论下。谢谢
【问题讨论】:
-
您需要对您的
Comments数组进行排序。另外,不要以大写开头命名您的 var。 -
@Larme 我将如何对其进行排序?可以举个例子吗
-
@Larme 就像我说的相互关联的 cmets 在 firebase 中具有相同的 ID,我会按它排序吗?
标签: ios arrays xcode uitableview