Search code examples
swifttableview

How to hide TableViewCells?


I have 2 arrays.

postList:[
    {id: 1, name: "json", content: "hi", sex: "male"},
    {id: 2, name: "bob", content: "hello", sex: "male"},
    {id: 3, name: "john", content: "wow", sex: "male"},
    {id: 3, name: "john", content: "unbelievable", sex: "male"}
    ]

blockList: [
    {id: 3, name: "john"},
    {id: 4, name: "allen"}
]

Tableview is made of postList Data. I want if blockList.id == postList.id, the cell should be hidden. How to compare 2 arrays and hide cells?


Solution

  • @hor Compare the array of data and create the result array

    Then use the result array to set data to the table view

    Refer below:

    let filteredList = postList.filter { dict in
        !blockList.contains(where: { blockDict in
            return dict["id"] as? Int == blockDict["id"] as? Int
        })
    }
        
    print(filteredList.map({ $0["name"] as? String }))
    

    From above example you can use filteredArray to set data to your table view