在 uitableview 单元格中显示项目的评分 [英] Show ratings of item in uitableview cell

查看:22
本文介绍了在 uitableview 单元格中显示项目的评分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 iOS 应用程序,该应用程序在 UITableView 中显示 30 个菜单项,然后用户可以在用户点击单元格时显示的详细信息页面上对这些项目进行评分(满分 5 分).

I am currently working on a iOS app that shows 30 menu items in a UITableView and Users can then rate theses items out of 5 starts on the detail page which is shown when a user taps the cell.

现在在单元格中当前只是项目的标题,现在我想要做的是拥有项目的标题和当前的平均值.对标题下方的项目进行评分.现在我知道如何创建单元格,但我无法以有效的方式获取电影当前评分的数据.

Now in the cell currently is just the title of the item, now what I want to do is have the title of the item and the current avg. rating of the item below the title. Now I know how to create the cell, but I am having trouble getting the data of the current rating of the movie in a efficient way.

我目前使用 Parse.com 作为我的后端.现在,当用户提交评分时,它会保存到我在 Parse 上的名为 Ratings 的课程中​​.并且保存了物品的标题和给定的星星数.

I am currently using Parse.com as my back-end. Now when a user submits a rating it is saved to my class on Parse called Ratings. And the title of the item and the number of stars given is saved.

现在我正在想最有效的方法来获取项目的平均星数并将其放入 UITableViewCell 中.首先,我想只执行一个云代码函数来查询所有 30 个项目,并平均评级就像在 parse.com docs https://parse.com/docs/cloud_code_guide#functions.

Now I am trying to think of the most efficient way to get the average number of stars for the item and put it in the UITableViewCell. First I thought about just performing a cloud code function that would query all 30 item, and average the ratings like is done in the parse.com docs https://parse.com/docs/cloud_code_guide#functions.

这里的问题是有 30 个或更多的项目,然后每个查询使用一个 API 请求,然后很快就会上升.所以我已经排除了这个选项.另一种方法是一次只显示 5 个项目,然后在用户向下滚动时加载下一个 5 个项目,但用户通常非常快速地滚动多个项目并且仍然会查看菜单上的所有 30 个项目,因此这并没有解决我的问题.

The problem here is that there are 30 or more items and each query then uses an API request which, then gets up high pretty quick. So I have ruled out that option. Another would be to only display 5 items at a time and then load then next 5 when the user scrolls down, but users typically scroll through multiple items very quickly and would still look at all 30 items on the menu, so that doesn't solve my problem.

现在我也有自己的服务器,我目前在其中存储一些正在提供的项目.我在想是否有办法制作一个 xml 文件或 JSON,当用户保存一个项目时,我将它保存到 Parse.然后也保存这个文件.

Now I also have my own server where I currently store some of the items being served. I was thinking if there is a way to make an xml file, or JSON, and when a user saves an item I save it to Parse. And then also save it this file.

所以这就是我的想法以及我陷入困境的地方.我会使用 Parse.com Javascript SDK 制作一个云代码作业,它会每天运行一次.我会让这个函数执行一个查询,检索在最后一天保存到我的 Ratings 类的所有评级.这会给我所有的对象.现在这就是我卡住的地方:如何从 Parse.com SDK 将此文件获取到我的服务器上,以及如何将新评级保存到此文件中?

So here is what I am thinking and where I get stuck. I would make a Cloud Code Job, using the Parse.com Javascript SDK, and it would run once a day. And I would have this function preform one query the retrieves all the ratings that were saved to my Ratings class in the last day. And this would give me all the objects. Now this is where I get stuck: how do I get this file onto my server from the Parse.com SDK and how do I just save the new ratings, to this file?

当然,我愿意接受所有其他解决方法的建议,并尽量减少我的 API 请求.我在想也可能使用 Parse.com 托管来托管 xml 或 JSON 文件?

Of course I am open to all other suggestions of ways to get around this and minimize my API requests. I was thinking also possibly use the Parse.com hosting to host the xml or JSON file?

或者只是一种完全不同的方法,我没有提到.

Or just a completely different way to do it, that I haven't mentioned.

我知道这是一个很长的问题,我已经尽力解释了,如果您有任何问题,请提出.

I know this is a long question, and I did my best to explain it, if you have any question please ask.

提前感谢您的帮助.我真的很感激:)

Thanks for the help in advance. I really appreciate it :)

编辑

我忘记提及的是,这些项目每天都在变化,而且我有多家餐厅,每家都有自己的项目,所以是 30 * # 个餐厅.因此,当我在表视图中显示 30 个项目时,我不确定如何在两个查询中获得所有评分(使用额外的类,我是否仍然需要查询每个项目)?

Something I forgot to mention is that these items change daily, and I have multiple restaurants each with their own items, so it's 30 * # of restaurants. So when I am displaying lets say 30 items in the table view Im not sure how I get all the ratings in two queries (with the extra class, don't I still need to query for each item)?

编辑 2

好的,现在我有一个 NSMutableArray 填充了评级,我从我的查询中返回了它.但是假设我的表视图中有一"、二"和三"项目.并且只对一"和三"进行了评级,然后我会返回这些项目的两个评级,如果在我的 cellForRowAtIndexPath: 方法中,我只是这样做.像

Okay so now I have an NSMutableArray filled with the ratings, that I get returned from my query. But lets say I have the items "one", "two" and "three" in my table view. And only "one" and "three" have been rated, then I get the two ratings back for those items, and if in my cellForRowAtIndexPath: method I just do. something like

cell.rating = ratingsArray[indexRow.path];

那么第二项的评分应该是三分...

Then the item two will have what should be threes rating...

你看到问题了吗?我该如何解决?

Do you see the problem? And how I could get around it?

谢谢

推荐答案

为什么不只拥有两个属性.allStars 和 numboerOfReviews.每次有人写评论,

Why not just have two properties. allStars and numboerOfReviews. Each time someone writes a review,

[movie incrementKey"allStars" 由用户选择]

[movie incrementKey:"numberOfReviews" byAmount:1]

然后处理平均客户端.allStars/numberOrReviews

Then handle avg client side. allStars/numberOrReviews

这些应该是您的 MovieObject 上的属性,并且在您查询电影时将包含它们,因为它们是基元而不是指针.因此,当您执行 PFQuery 时,如果成功,您将获得一个 PFObject 数组.将 tableViewDataArray 设置为查询返回的数组.然后在 cellForRowAtIndexPath 中,访问属性,例如 (tableViewDataArray[indexPath.row] as Movie).numberOfStars

These should be properties on your MovieObject, and they will be included when you query the movies, since they are primitives and not pointers. So when you perform a PFQuery, if successful, you get an array of PFObjects. Set you tableViewDataArray to the array your query returns. Then in cellForRowAtIndexPath, you access you properties, like (tableViewDataArray[indexPath.row] as Movie).numberOfStars

这篇关于在 uitableview 单元格中显示项目的评分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆