swift - 使用 NSPredicate 将启用用户添加到 'Favourite' 对象,然后该对象将出现在单独的 TableView 中(没有 CoreData!)

标签 swift xcode sorting nspredicate predicate

我目前有一个 tableView,它正在解析和显示 JSON 文件中的数据。当按下某个单元格时,用户将进入 detailView 屏幕以查看更多信息。

我现在尝试在 detailView 中设置一个 Favourite 按钮,以便用户可以按下一个按钮,将对象添加到 favouriteView (其设置与 tableView 完全相同,但仅显示已收藏的位置。

我的理解是,实现此目的的最佳方法是使用 NSPredicate。我已经环顾了几个小时,似乎找不到不使用 CoreData 的解决方案(尽管我找到了一个消息来源,表明不需要 CoreData )。我想在不使用CoreData的情况下实现Favourite功能。

我已经在 detailView 中设置了一个收藏夹按钮,它将变量 isFavouritefalse 设置为 true.请参阅下面的所有相关代码。

示例 JSON 对象:

{
        "locations": [

            {
                "id": "0001",
                "name": "Helensburgh Tunnels",
                "type": "Tunnels, Beach, Views",
                "location": "Helensburgh, South Coast",
                "image": "Helensburgh-Tunnels.jpg",
                "activity": "Walking, photography, tunnelling",
                "isVisited": false,
                "isFavourite": false,
                "latitude": -34.178985,
                "longitude": 150.992867
            }
        ]
    }

我在detailView中设置的收藏夹按钮:

@IBOutlet weak var favouriteButton: UIButton!
@IBAction func favouriteButtonTapped(_ sender: Any) {
    self.location.isFavourite = self.location.isFavourite ? false : true

    print(location.isFavourite)

    // Favourite button
    if location.isFavourite == false {
        favouriteButton?.setImage(UIImage(named: "starLine.png"), for: .normal)
    } else {
        favouriteButton?.setImage(UIImage(named: "starFull.png"), for: .normal)
    }

}

如何获取已设置为 isFavourite = true 的所有对象并将其显示在 tableView 中?

这是 FavouriteLocationsViewController 类的一部分,我需要在此处修复什么才能使其正常工作?

class FavouriteLocationsViewController: UITableViewController {

    var locations = [Location]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Remove the title of the back button
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

        if let locationJson = readLocation(){
            if let locationArray = locationJson["locations"] as? [[String:Any]]{
                for location in locationArray{
                    locations.append(Location.init(locationInfo: location))
                }
                print(locations.count)
                self.tableView.reloadData()
            }
        }

    }

    // Filter to show only favourite locations
    func filterArrayOfDictonaryWithKeyValueUsingPredicate(arrOfDict: NSArray, keyOfDict: String, strSearchText: String) -> NSArray{
    let favouritePredicate = NSPredicate(format: "isFavourite == true")
    let arrResult = arrOfDict.filtered(using: favouritePredicate)
    return arrResult as NSArray
    }
}

最佳答案

您只想在那里显示最喜欢的数据吗?

您有如上面所示的字典数组。

您可以使用谓词过滤该数组。

你只能得到 isFavourite == "True"的对象

使用下面的函数传递 Location 数组,传递 keyofDict - isFavourite

strSearchText - 对于您的示例,请使用true

func filterArrayOfDictonaryWithKeyValueUsingPredicate(arrOfDict: NSArray, keyOfDict: String, strSearchText: String) -> NSArray{
        let searchPredicate = NSPredicate(format: "\(keyOfDict) CONTAINS[C] %@", strSearchText)
        let arrResult = arrOfDict.filtered(using: searchPredicate)
        return arrResult as NSArray
    }

关于swift - 使用 NSPredicate 将启用用户添加到 'Favourite' 对象,然后该对象将出现在单独的 TableView 中(没有 CoreData!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44923702/

相关文章:

ios - addressDictionary 随机返回 nil

ios - 在 View Controller 之间传递数据

python - Pandas 数据帧 : Sort list column in dataframe

ios - 转到类(class)时mp3播放失败

ios - HMCharacteristicWriteAction 为 targetValue 传递什么

python - 使用 numpy.argpartition 忽略 NaN

sorting - 如何对map reduce hadoop中的数据进行排序?

swift - tvos swift spritekit - UITapGestureRecognizer 没有响应

ruby - 找不到从 swift 中执行 ruby​​ 脚本的方法

xcode - 如何在 UITextField 上设置字符限制