[英]Restrict table view unless select all cell in swift
我有一个表格视图,其中正在填充从服务中获取的数据。 数据是完全动态的,并且表视图包含其下的节和单元,所有事物都是动态的。 我在表格视图外有一个按钮操作,用于添加选定的单元格数据。 现在,我想限制按钮,直到选择了部分下的所有单元格,才添加数据。 我希望用户首先检查单元格,然后通过添加按钮添加。 我的表格视图代码是这样的,
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 return cell}
基本上,您必须根据用户选择了该行或取消选择该行来设置选择的是与否,然后只需检查数据集中是否已选择任何内容(如果是),然后将按钮突出显示/启用,否则将禁用/取消突出显示
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
}
}
我创建了演示,假设您有两个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
有两个IBOutlets
@IBOutlet weak var lblTemp: UILabel!
@IBOutlet weak var btnRadio: UIButton!
仅供参考。 请设置btnRadio
default
并正确selected
图像。
您的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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.