gpt4 book ai didi

ios - 限制表格 View ,除非快速选择所有单元格

转载 作者:行者123 更新时间:2023-11-30 11:28:23 25 4
gpt4 key购买 nike

我有一个 TableView ,其中填充从我的服务获取的数据。数据是完全动态的,表格 View 包含其下的部分和单元格,所有内容都是动态的。我在 TableView 之外有一个按钮操作,用于添加选定的单元格数据。现在我想限制按钮,使其在选择部分下的所有单元格之前不会添加数据。我希望用户首先检查单元格,然后通过添加按钮添加。我的表格 View 代码是这样的,

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

最佳答案

基本上,您必须根据用户选择的行或取消选择的行来设置选定的 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
}
}

关于ios - 限制表格 View ,除非快速选择所有单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50501607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com