首页 > 解决方案 > 当对象具有相同的值时,如何在我的 tableView 中显示 JSON 数据?

问题描述

我正在尝试根据具有相同值的对象在我的 tableview 单元格中显示 JSON 数据。

现在,我可以将所有 JSON 数据显示到我的单元格行中,但我无法排除不具有相同对象值的数据。

我正在使用此设置来获取数据:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // Retrieve cell
        let cellIdentifier: String = "takesTableViewCell"
        let myCell: TakesTableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)! as! TakesTableViewCell

        // Get the location to be shown
        let item: LocationModel = feedItems[indexPath.row] as! LocationModel

        // Get references to labels of cell
        myCell.mainNameLabel.text = item.title
        myCell.mainTakeLabel.text = item.take

        return myCell
    }

这是我拥有的示例 JSON 数据

[
   {
      "id":"1",
      "title":"Great Title 1",
      "take":"Testing !!!",
      "movieid":"299534"
   },
   {
      "id":"2",
      "title":"Great Title 2",
      "take":"Testing 1,2,3",
      "movieid":"299666"
   },
   {
      "id":"3",
      "title":"Great Title 3",
      "take":"Testing 1,2,3",
      "movieid":"299534"
   }
]

我只想显示具有相同“movieid”的结果,如下所示:

标题:伟大的标题 1
采取:测试!

Title: Great Title 3
Take:测试 1,2,3

标签: jsonswiftuitableview

解决方案


在后端,您只想返回 movieid 数量计数 > 1 的记录

SELECT *
FROM movie_table
GROUP BY movie_id
HAVING COUNT(movie_id) > 1;

用于查找 count > 1 的记录的 SQL 查询


推荐阅读