如何组织tableview单元格

时间:2018-07-16 12:03:07

标签: ios arrays xcode uitableview

我在一个表视图中有两种不同类型的表视图单元格。第一个单元格将原始评论打印到帖子,第二个单元格将评论打印到另一个评论。当前,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”,我将如何组织单元格,以便将主注释的注释列在原始注释的下面。谢谢

1 个答案:

答案 0 :(得分:1)

您可以创建一个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”的对象都包含主要的注释信息以及与此相关的回复信息列表,您可以使用它来管理数据。