如何删除表格视图单元格中单选按钮的索引路径?
Posted
技术标签:
【中文标题】如何删除表格视图单元格中单选按钮的索引路径?【英文标题】:how to remove index path for the radio button in table view cell? 【发布时间】:2017-09-05 04:53:08 【问题描述】:这里我在启用单选按钮后有一个单选按钮,然后在选择删除特定单元格后会出现删除和编辑按钮,但前一行单选按钮启用了如何避免这种情况有任何帮助?
这里是这个的代码
@IBAction func selectRadioButton(_ sender: KGRadioButton)
let chekIndex = self.checkIsRadioselect.index(of: sender.tag)
_ = self.checkIsButtonEnable.index(of: sender.tag)
if sender.isSelected
else
if(chekIndex == nil)
self.checkIsRadioSelect.removeAll(keepingCapacity: false)
self.checkIsRadioSelect.append(sender.tag)
self.checkIsButtonEnable.removeAll(keepingCapacity: false)
self.checkIsButtonEnable.append(sender.tag)
self.tableDetails.reloadData()
self.addressSelected = true
tableDetails.tableFooterView?.isHidden = false
tableDetails.reloadData()
func deleteAction(button: UIButton)
let buttonPosition = button.convert(CGPoint(), to: tableDetails)
let index = tableDetails.indexPathForRow(at: buttonPosition)
self.tableDetails.beginUpdates()
shippingArray.remove(at:(index?.row)!)
self.tableDetails.deleteRows(at: [index!], with: .top)
self.tableDetails.endUpdates()
tableDetails.tableFooterView?.isHidden = true
self.addressSelected = false
self.tableDetails.reloadData()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
if (indexPath.section == 0)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AddressTableViewCell
tableDetails.isHidden = false
myActivityIndicator.stopAnimating()
let arr = shippingArray[indexPath.row]
cell.deleteButton.tag = indexPath.row
cell.nameLabel.text = arr["name"] as? String
cell.addressLabel.text = arr["address"]as? String
let mobilenumber : Any = arr["number"] as AnyObject
cell.mobileNumberLabel.text = "\(mobilenumber)"
cell.radioButton.tag = indexPath.row
cell.editButton.tag = indexPath.row
cell.deleteButton.tag = indexPath.row
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
cell.deleteButton.addTarget(self, action: #selector(deleteAction(button:)), for: .touchUpInside)
let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
if(checkIndex != nil)
cell.radioButton.isSelected = true
cell.editButton.isHidden = false
cell.deleteButton.isHidden = false
else
cell.radioButton.isSelected = false
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
return cell
【问题讨论】:
您已经将标签值设置为删除按钮 -> cell.deleteButton.tag = indexPath.row in cellForRowAt。在您的删除方法中访问该标记值并使用 button.tag 值删除数组中的值 如何删除我不明白? @AshwiniChougale 简单,当您删除行时,也会从所选数组中删除该索引。您正在从 indexpath.row(eg.3) 中删除数据并重新加载。所以基本上在删除 3 行队列刷新并且 (4 --> 3) 4 索引数据移动到 3 之后,你总共会少一个项目。 只是你需要做的-> shippingArray.remove(at:(button.tag)) 【参考方案1】:在删除方法中添加两行。
func deleteAction(button: UIButton)
let buttonPosition = button.convert(CGPoint(), to: tableDetails)
let index = tableDetails.indexPathForRow(at: buttonPosition)
self.tableDetails.beginUpdates()
shippingArray.remove(at:(index?.row)!)
self.tableDetails.deleteRows(at: [index!], with: .top)
self.tableDetails.endUpdates()
tableDetails.tableFooterView?.isHidden = true
self.addressSelected = false
**let checkIndex = self.checkIsRadioSelect.index(of: button.tag)
self.checkIsRadioSelect.remove(at:checkIndex)**
self.tableDetails.reloadData()
【讨论】:
欢迎,它的妈妈以上是关于如何删除表格视图单元格中单选按钮的索引路径?的主要内容,如果未能解决你的问题,请参考以下文章
如何在基于部分的表格视图中获取开关更改事件中单元格的索引路径