如何在iOS swift中实现评论和回复系统
我必须在我的应用程序中实现评论和回复系统。 我从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都是相同的回复和评论评论的评论将等于回复的parentReviewId。 如果回复已经存在,则应该在下面显示相应的注释,如果没有回复,我们应该可以通过单击注释单元格中的“回复”按钮来添加回复。 这是概念,我真的尝试过它,我不能这样做。 希望你们能帮助我做到这一点..感谢提前任何帮助..
以下是它应该如何显示的示例屏幕:
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
键中有对该注释的相关回复。