【问题标题】:How to Hide specific UIButtons based on String contained in Array如何根据数组中包含的字符串隐藏特定的 UIButton
【发布时间】:2019-03-17 02:48:54
【问题描述】:

我有一些 UIButtons 代表不同的餐厅,内容来自一个结构。在这个结构中是一个带有每个餐厅标签的数组。 如何根据标签隐藏 UIButton? 目前我暂时有这个:

func filterFavorites() {
    if importedDataA.filterTags.contains(filterPresetRestaurantTypeService) {
        isVisibleA = true
    } else {
        isVisibleA = false
    }
    if importedDataB.filterTags.contains(filterPresetRestaurantTypeService) {
        isVisibleB = true
    } else {
        isVisibleB = false
    }
    if importedDataC.filterTags.contains(filterPresetRestaurantTypeService) {
        isVisibleC = true
    } else {
        isVisibleC = false
    }
    if importedDataD.filterTags.contains(filterPresetRestaurantTypeService) {
        isVisibleD = true
    } else {
        isVisibleD = false
    }
    if importedDataE.filterTags.contains(filterPresetRestaurantTypeService) {
        isVisibleE = true
    } else {
        isVisibleE = false
    }
    if importedDataF.filterTags.contains(filterPresetRestaurantTypeService) {
        isVisibleF = true
    } else {
        isVisibleF = false
    }
    if importedDataG.filterTags.contains(filterPresetRestaurantTypeService) {
        isVisibleG = true
    } else {
        isVisibleG = false
    } 

等等……

还有……

func filterApply() {
    if isVisibleA == true {
        if UserDefaults.standard.bool(forKey: "hideFilteredObjects") == true {
            cellA.isHidden = false
        } else {
            //cellA.popIn()
        }
    } else {
        if UserDefaults.standard.bool(forKey: "hideFilteredObjects") == true {
            cellA.isHidden = true
        } else {
            //cellA.popOut()
        }
    }
}

【问题讨论】:

  • 你能建立一个标签和按钮的字典,所以对于每个标签,你可能有一个显示或隐藏的按钮列表(根据你想要的操作),然后你可以简单地遍历按钮列表并应用了所需的操作
  • 使用 isVisibleA = importedDataA.filterTags.contains(filterPresetRestaurantTypeService) 而不是 if else。

标签: ios arrays swift struct


【解决方案1】:

您可以创建一个实例数组,例如:

var importedData: [your-structure] = []

然后,您可以使用 for 循环来遍历这些实例,而不是使用 if 语句逐个遍历所有这些项目:

for data in importedData{
  if data.filterTags.contains(filterPresetRestaurantTypeService){
    isVisibleG = true
  } else {
    isVisibleG = false
  } 
}

【讨论】:

    【解决方案2】:

    假设你有一个结构:

    struct ImportedData{
        var filterTags: [Int]
    }
    
     let buttons = [UIButton]() // add your buttons
    
     let importedData = [ImportedData]() // add your importedData
    
     (0..<(buttons.count)).map{buttons[$0].isHidden =     UserDefaults.standard.bool(forKey: "hideFilteredObjects") && (!importedData[$0].filterTags.contains(filterPresetRestaurantTypeService))}
    

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 1970-01-01
      • 2013-07-26
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 2021-10-23
      • 2010-11-16
      相关资源
      最近更新 更多