0

I am trying to display JSON data in my tableview cell based on objects with the same value.

Right now, I am able to display ALL the JSON data into my cell rows, but I am unable to exclude data that doesn't have the same object value.

I'm using this setup to get the data:

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
    }

Here's sample JSON data I have

[
   {
      "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"
   }
]

I only want to display the results that have the same "movieid" like so:

Title: Great Title 1
Take: Testing !!!

Title: Great Title 3
Take: Testing 1,2,3

rmaddy
  • 314,917
  • 42
  • 532
  • 579
gdeleon101
  • 98
  • 1
  • 2
  • 11
  • Not really clear on some points. Is there only one movieid that will be duplicated? Do you know what it is in advance? What happens if there are none? What if there is more than one? In any case, this will take some preprocessing. The tableView functions will not filter for this automatically. Maybe build a dictionary with movieid as the key, and value is an array of objects, then iterate over keys looking for an array with count > 1? – GntlmnBndt May 07 '19 at 22:13
  • There could be multiple movieid's that can be duplicated. And some might not be duplicated. I also don't know which movieids will be duplicated beforehand. How do I create a dictionary? This JSON is created from my PHP file which converts items in a mysql database table to JSON. The Database is populated from an iOS app post. – gdeleon101 May 07 '19 at 22:23

2 Answers2

2

In the backend you only want to return records that have a movieid quantity count > 1

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

SQL query for finding records where count > 1

Mocha
  • 2,035
  • 12
  • 29
  • See, you give me swift code, and that is all I think about. This is a better solution. – GntlmnBndt May 07 '19 at 23:42
  • Problem is, I will have to create numerous swift files to call different backend files. I was hoping to have one call to the database in a swift file, then have it set up to where another viewcontroller will pull specific data it needs from that swift file depending on the view it is on. – gdeleon101 May 08 '19 at 02:23
  • Also, the JSON will be dynamic, I won't be able to know what the ids are beforehand. I'm using the movie database API. The the user will be able to click on a movie which has its unique id. They then click on a button to create a "take" about the movie, which goes to the database. I want to retrieve those specific takes based on movie id. – gdeleon101 May 08 '19 at 02:31
  • Why do you only want duplicate takes for a movie id? I don't know what "the movie database API" is. Do you have access to configure these APIS? If so, I would recommend you creating a new API to produce the query I posted. If you have to do this all client side, you will need to implement a dictionary to get the counts as @GntlmnBndt answered. – Mocha May 08 '19 at 16:04
1

Start by converting the array of objects into a dictionary.

var movieDict: [String:[[String:String]]] = []
for movObj in jsonData {
    guard let key = movObj["movieid"] else { continue }
    var objArr = movieDict[key] ?? []
    objArr.append(movObj)
    movieDict[key] = objArr
}

Then you can look through to see which ones have multiple objects with the same movieid.

for (k, v) in movieDict {
    if v.count > 0 {
        // either track the keys, or tableData = v
    }
}
GntlmnBndt
  • 249
  • 2
  • 6