除非在swift中选择所有单元格,否则限制表视图

时间:2018-05-24 05:28:07

标签: ios swift uitableview

我有一个表格视图,其中我填充从我的服务获取的数据。数据是完全动态的,表视图包含部分和单元格,所有的东西都是动态的。我在表视图外面有一个按钮操作,用于添加选定的单元格数据。现在我想限制它不添加数据的按钮,直到选中部分下的所有单元格。我希望用户首先检查单元格,然后通过添加按钮添加。我的表视图代码就是这个,

func numberOfSections(in tableView: UITableView) -> Int {

        return AddonCategoryModel!.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

         return AddonCategoryModel![section].name
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 34
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

         return 50
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return AddonCategoryModel![section].addonItems.count
}

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

        let cell = addonTableView.dequeueReusableCell(withIdentifier: "addonCell", for: indexPath) as! RestaurantMenuDetailAddonTVC

        cell.addonTitleLbl.text = AddonCategoryModel![indexPath.section].addonItems[indexPath.row].name
        cell.priceLbl.text = String(AddonCategoryModel![indexPath.section].addonItems[indexPath.row].price)


        if selection[indexPath.section].isSelected[indexPath.row] {
             cell.radioBtn.setImage(UIImage (named: "radio"), for: UIControlState.normal)
          addonItemName = cell.addonTitleLbl.text!
          addonItemprice = AddonCategoryModel![indexPath.section].addonItems[indexPath.row].price
          addonItemId = AddonCategoryModel![indexPath.section].addonItems[indexPath.row].addonPKcode
          addonItemNameArray.append(addonItemName)
          addonItemPriceArray.append(addonItemprice)
          addonItemIdArray.append(addonItemId)

          let defaults = UserDefaults.standard
            defaults.set(addonItemName, forKey: "addonItemName")
            defaults.set(addonItemprice, forKey: "addonItemPrice")
            defaults.set(addonItemId, forKey: "addonItemId")

            defaults.synchronize()

        }
        else {

            cell.radioBtn.setImage(UIImage (named: "uncheckRadio"), for: UIControlState.normal)
        }

        cell.radioBtn.tag = indexPath.row

// cell.radioBtn.addTarget(self,action:#selector(checkBoxSelection(_ :)),for:.touchUpInside)             cell.selectionStyle = .none             cell.backgroundColor = UIColor.clear             返回细胞     }

我的屏幕看起来像这样, enter image description here

2 个答案:

答案 0 :(得分:0)

基本上,您必须根据用户选择行或取消选择行来设置所选的true和false,然后只需检查您的数据集是否选中任何内容然后使按钮突出显示/启用其他禁用/取消突出显示

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selection[indexPath.section].isSelected = true
    tableView.reloadData()
    CheckIfAnyOneIsSelected()
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
   selection[indexPath.section].isSelected = false
   tableView.reloadData()
   CheckIfAnyOneIsSelected()
}

func CheckIfAnyOneIsSelected() {
    //loop through your array and check if anyone is selected if yes break the loop and set the button to enable
    //else make the button disable

   var anyOneSelecte = false
   for singleModel in AddonCategoryModel {
      for item in addonItems {
        if item.isSelected == true 
        anyOneSelecte = true
        break;
      }
   }

   if anyOneSelecte {
      //enable your button 
   } else {
     //disable your button
   }
}

答案 1 :(得分:0)

我创建了demo,让我们说你有两个Model类,

class AddOnCategoryModel {
    var name: String = ""
    var arrValues = [Category]()

    init(name: String) {
        self.name = name
    }
}

class Category {
    var name: String = ""
    var price : String = ""
    var isSelected: Bool = false
}

以下是mainArray

    for i in 0...2 {
        let model = AddOnCategoryModel(name: "Section \(i)")
        for j in 0...3 {
            let cate = Category()
            cate.name = "Category \(j)"
            model.arrValues.append(cate)
        }
        mainArray.append(model)
    }

现在考虑您已关注ListTableCell

enter image description here

有两个IBOutlets

@IBOutlet weak var lblTemp: UILabel!
@IBOutlet weak var btnRadio: UIButton!

FYI。请正确设置btnRadio defaultselected图片。

您的UITableViewDataSource方法,

func numberOfSections(in tableView: UITableView) -> Int {
    return mainArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mainArray[section].arrValues.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableCell") as! ListTableCell
    let category = mainArray[indexPath.section]
    cell.lblTemp.text = category.arrValues[indexPath.row].name
    cell.btnRadio.tag = indexPath.row
    cell.tag = indexPath.section
    cell.btnRadio.addTarget(self, action: #selector(btnRadioTapped), for: .touchUpInside)
    return cell
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return mainArray[section].name
}

请找到btnRadioTapped方法,

@objc func btnRadioTapped(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected

    let cell = sender.superview?.superview as! ListTableCell

    let addOnModel = mainArray[cell.tag]
    let category = addOnModel.arrValues[sender.tag]
    category.isSelected = sender.isSelected
}

不要在这样的按钮点击事件中选中或不选中所有复选框

@IBAction func btnTapped(_ sender: UIButton) {
    var isCheckedAll = true
    for (_ , item) in mainArray.enumerated() {
        let value = item.arrValues.filter({$0.isSelected==false})
        if value.count > 0 {
            isCheckedAll = false
            break;
        }
    }

    print("Done ", isCheckedAll)
}

如果选择了所有radioButtons,它将返回true,如果未选择任何radioButton,则返回false。

如有任何疑问,请与我联系。这只是演示,您必须根据最终要求进行细微更改。

<强>更新

请查看下面的didSelectRowAt indexPath方法,

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let model = mainArray[indexPath.section]
    let category = model.arrValues[indexPath.row]
    category.isSelected = !category.isSelected

    let cell = tableView.cellForRow(at: indexPath) as! ListTableCell
    cell.btnRadio.isSelected = category.isSelected
}