• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何在 iOS swift 中实现评论和回复系统

[复制链接]
菜鸟教程小白 发表于 2022-12-13 04:16:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我必须在我的应用程序中实现评论和回复系统。我正在从 API 获取数据,但我不明白如何执行此操作,因为我以前没有做过类似的事情。我知道我必须为这种类型的系统使用两个不同的单元格,但我不知道如何过滤评论和回复的数据并将它们显示在各自的单元格中。

这是我的 JSON:

[
    {
        "firstName": "Mark",
        "lastName": "King",
        "profilePic": "avatar2 - Copy - Copy.jpg",
        "reviewId": 54,
        "userId": 4,
        "serviceProviderId": 0,
        "rating": 0,
        "description": "Thank You...",
        "isDeleted": false,
        "parentReviewId": 53,
        "createdDate": "2018-05-08T12:32:43.033",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 62,
        "startDate": "2018-05-08T12:29:00",
        "endDate": "2018-05-08T14:29:00",
        "serviceName": "Security",
        "businessId": 0,
        "businessProfilePic": ""
    },
    {
        "firstName": "Satya Indra",
        "lastName": "varada",
        "profilePic": "3354757569.jpg",
        "reviewId": 53,
        "userId": 9,
        "serviceProviderId": 0,
        "rating": 4,
        "description": "nice service...",
        "isDeleted": false,
        "parentReviewId": 0,
        "createdDate": "2018-05-08T12:31:44.313",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 62,
        "startDate": "2018-05-08T12:29:00",
        "endDate": "2018-05-08T14:29:00",
        "serviceName": "Security",
        "businessId": 0,
        "businessProfilePic": ""
    },
    {
        "firstName": "Mark",
        "lastName": "King",
        "profilePic": "avatar2 - Copy - Copy.jpg",
        "reviewId": 52,
        "userId": 4,
        "serviceProviderId": 0,
        "rating": 0,
        "description": "sorry for the trouble",
        "isDeleted": false,
        "parentReviewId": 9,
        "createdDate": "2018-05-07T11:52:23.81",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 16,
        "startDate": "2018-04-24T11:30:00",
        "endDate": "2018-04-24T12:30:00",
        "serviceName": "Cleaning",
        "businessId": 2,
        "businessProfilePic": "logo-home.png"
    },
    {
        "firstName": "James",
        "lastName": "Miller",
        "profilePic": "avatar.jpg",
        "reviewId": 9,
        "userId": 7,
        "serviceProviderId": 0,
        "rating": 2,
        "description": "bad",
        "isDeleted": false,
        "parentReviewId": 0,
        "createdDate": "2018-04-24T23:59:06.357",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 16,
        "startDate": "2018-04-24T11:30:00",
        "endDate": "2018-04-24T12:30:00",
        "serviceName": "Cleaning",
        "businessId": 2,
        "businessProfilePic": "logo-home.png"
    },
    {
        "firstName": "Mark",
        "lastName": "King",
        "profilePic": "avatar2 - Copy - Copy.jpg",
        "reviewId": 2,
        "userId": 4,
        "serviceProviderId": 0,
        "rating": 0,
        "description": "Thank you",
        "isDeleted": false,
        "parentReviewId": 1,
        "createdDate": "2018-04-20T14:57:29.073",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 2,
        "startDate": "2018-04-25T00:00:00",
        "endDate": "2018-04-26T09:00:00",
        "serviceName": "Security",
        "businessId": 0,
        "businessProfilePic": ""
    },
    {
        "firstName": "James",
        "lastName": "Miller",
        "profilePic": "avatar.jpg",
        "reviewId": 1,
        "userId": 7,
        "serviceProviderId": 0,
        "rating": 4,
        "description": "Great",
        "isDeleted": false,
        "parentReviewId": 0,
        "createdDate": "2018-04-20T14:56:57.34",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 2,
        "startDate": "2018-04-25T00:00:00",
        "endDate": "2018-04-26T09:00:00",
        "serviceName": "Security",
        "businessId": 0,
        "businessProfilePic": ""
    }
]

这里所有的字典都代表一个数组中的评论和评论。不同之处在于任何评论的 bookingId 将与回复相同,评论的 reviewId 将等于回复的 parentReviewId。如果已经有回复,则应在下方显示相应的评论,如果没有回复,我们应该能够通过单击评论单元格中的回复按钮来添加它。这就是这个概念,我真的试过了,我做不到。希望你们能帮助我做到这一点..提前感谢您的帮助..



Best Answer-推荐答案


API 响应的最佳实践应该是,回复应该在评论字典中。喜欢:

{
    "firstName": "James",
    "lastName": "Miller",
    "profilePic": "avatar.jpg",
    "reviewId": 9,
    "userId": 7,
    "serviceProviderId": 0,
    "rating": 2,
    "description": "bad",
    "isDeleted": false,
    "parentReviewId": 0,
    "createdDate": "2018-04-24T23:59:06.357",
    "modifiedDate": "0001-01-01T00:00:00",
    "bookingId": 16,
    "startDate": "2018-04-24T11:30:00",
    "endDate": "2018-04-24T12:30:00",
    "serviceName": "Cleaning",
    "businessId": 2,
    "businessProfilePic": "logo-home.png"
    "replies": [
            {
                "firstName": "Mark",
                "lastName": "King",
                "profilePic": "avatar2 - Copy - Copy.jpg",
                "reviewId": 52,
                "userId": 4,
                "serviceProviderId": 0,
                "rating": 0,
                "description": "sorry for the trouble",
                "isDeleted": false,
                "parentReviewId": 9,
                "createdDate": "2018-05-07T11:52:23.81",
                "modifiedDate": "0001-01-01T00:00:00",
                "bookingId": 16,
                "startDate": "2018-04-24T11:30:00",
                "endDate": "2018-04-24T12:30:00",
                "serviceName": "Cleaning",
                "businessId": 2,
                "businessProfilePic": "logo-home.png"
            },
            {
                "firstName": "Mark",
                "lastName": "King",
                "profilePic": "avatar2 - Copy - Copy.jpg",
                "reviewId": 60,
                "userId": 4,
                "serviceProviderId": 0,
                "rating": 0,
                "description": "sorry for the trouble new",
                "isDeleted": false,
                "parentReviewId": 9,
                "createdDate": "2018-05-07T11:52:23.81",
                "modifiedDate": "0001-01-01T00:00:00",
                "bookingId": 16,
                "startDate": "2018-04-24T11:30:00",
                "endDate": "2018-04-24T12:30:00",
                "serviceName": "Cleaning",
                "businessId": 2,
                "businessProfilePic": "logo-home.png"
            }
        ]
}

但在当前情况下,您可以检查 parentReviewId。如果为0,则为回复,否则为评论。你可以这样:

    let responseArray = [[String: Any]]() // This is array fro your question

    // Extract commentArray
    let commentArray = responseArray.filter( { Int(exactly: $0["parentReviewId"] as! NSNumber) == 0  } )
    // Load updatedResponseArray with relpies into it
    var updatedResponseArray = [[String: Any]]()

    for itemDict in commentArray {
        let reviewId = Int(exactly: itemDict["reviewId"] as! NSNumber) ?? 0
        let replies = responseArray.filter( { Int(exactly: $0["parentReviewId"] as! NSNumber) == reviewId  } )

        var dict = itemDict
        dict["replies"] = replies
        updatedResponseArray.append(dict)
    }

    print(updatedResponseArray)

现在,您可以使用此 updatedResponseArray,字典中的主要详细信息是评论,并且您在 replies 键中有对该评论的相关回复。

关于ios - 如何在 iOS swift 中实现评论和回复系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50312995/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap