如何组织表格视图单元格

Posted

技术标签:

【中文标题】如何组织表格视图单元格【英文标题】:how to organize tableview cells 【发布时间】: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,我会按它排序吗? 【参考方案1】:

您可以创建一个 Comment 自定义对象类,该类将包含一组子 cmets 和主要评论,以正确安排或管理您的数据结构。之后,您可以在表格视图单元格中正确使用它。 好的,例如,您可以拥有以下数据结构。

创建一个评论类:

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”类型的对象都包含主要的评论信息以及回复信息列表,这样你就可以管理你的数据了。

【讨论】:

你能举个例子吗,我将如何在一个对象类中组织两个数组 你能告诉我更多关于你拥有的数据结构吗?你如何找到评论及其子cmets?什么是replyId = post["id"]?字符串,让replyTo = post["reply_to"] 为?字符串并让 postID = post["post_id"] as?字符串? ***.com/questions/51372955/… 我刚刚发布的链接基本上是在问同样的问题,并且我对我的代码进行了更深入的描述。 请检查我的回答。现在我已经编辑了,我添加了一个例子来解释它。

以上是关于如何组织表格视图单元格的主要内容,如果未能解决你的问题,请参考以下文章

如何在表格视图中显示表格单元格的详细视图?

如何更改分组的表格视图单元格的背景?

如何在一个表格视图中添加三个表格视图单元格作为子视图?

由于标签栏挡住了单元格,如何设置表格视图单元格的位置?

如何根据集合视图中的单元格选择来控制表格视图的内容?

如何在编辑表格视图单元格中的文本字段时重新加载所有表格视图单元格 - iOS Swift