首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS中实现评论回复系统

问如何在iOS中实现评论回复系统
EN

Stack Overflow用户
提问于 2018-05-13 05:20:57
回答 1查看 1.2K关注 0票数 2

我必须在我的申请中实施评论和回复系统。我正在从API中获取数据,我不知道如何做到这一点,因为我以前从未做过类似的事情。我知道,对于这种类型的系统,我必须使用两个不同的单元格,但我不知道如何过滤评论和回复的数据,并在各自的单元格中显示它们。

下面是我的JSON:

代码语言:javascript
复制
[
    {
        "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。如果一个回复已经存在,它应该显示在下面,它是相应的注释,如果没有回复,我们应该能够通过单击注释单元格中的reply按钮来添加它。这就是我的想法,我真的试过了--我做不到。希望你们能帮我..。谢谢你提前提供帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-13 07:09:50

API响应的最佳实践应该是类似的,回复应该在注释字典中。比如:

代码语言:javascript
复制
{
    "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。如果是,则为回复,否则为注释。你可以这样做:

代码语言:javascript
复制
    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键中有对该注释的相关答复。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50312995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档