【问题标题】:Not able to get single selection checkbox无法获得单选复选框
【发布时间】:2019-08-05 05:35:35
【问题描述】:

我制作了一个 tableviewcell,里面有一个按钮。我在 tableviewcell.swift 类中连接了按钮的 IBAction。然后我做了一个委托并像这样访问 viewcontroller 类中的按钮触摸操作..

func optionTap(cell: SlideUpTableViewCell) {
    if let indexPath = tableview?.indexPath(for: cell) {
     }
}

但我想要实现的是,我希望一次只选择一个复选框按钮。我有 3 行,现在我可以点击 3 个按钮中的每一个,而我一次只想点击 1 个按钮。即,如果我点击第一个单元格上的按钮,然后点击第二个单元格中的按钮,则应自动取消选择第一个单元格按钮。简而言之,正常的单选是如何工作的……

这个我试过了...但是不行...

   func optionTap(cell: SlideUpTableViewCell) {
if let indexPath = tableview?.indexPath(for: cell) {

  if selectedArrayIndex.contains(indexPath.row) {

    selectedArrayIndex.remove(at: selectedArrayIndex.index(of: indexPath.row)!)
    cell.checkBobButton.tintColor = ThemeManager.current().inactiveGrayColor
    cell.checkBobButton.setImage(UIImage(named: "circle_stroke"), for: .normal)

  } else {
    selectedArrayIndex.append(indexPath.row)
    cell.checkBobButton.tintColor = ThemeManager.current().secondaryColor
    cell.checkBobButton.setImage(UIImage(named: "circle_tick"), for: .normal)

  }

 }
}

编辑 1: 这是 cellForRow..

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: SlideUpTableViewCell = tableView.dequeueReusableCell(withIdentifier: cellID) as! SlideUpTableViewCell
    cell.delegate = self
    cell.selectionStyle = .none
    cell.headingLabel.text = self.arrData[indexPath.row].heading
    cell.subHeadingLabel.text = self.arrData[indexPath.row].subHeading

    return cell
  }

编辑 2 这就是 UI 的外观......

【问题讨论】:

  • 添加一些额外的代码
  • 好的@Anbu.Karthik ..但这就是我的困惑..:)
  • 显示你的 cellforRow
  • 我已经用 cellForRow..@Anbu.Karthik 编辑了问题
  • @asd2,对于单选使用 didSelectRowAtIndex 路径函数。

标签: ios swift uitableview uibutton


【解决方案1】:

首先,您不需要按钮操作。按照以下步骤进行简单而完美的编码:

  1. 在你的控制器中取一个变量var selectedIndex = -1 // -1 如果没有选择任何索引,如果你想默认选择任何选项然后相应地改变它。

  2. 使用tableView委托方法didSelectRowAt indexPath

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    
        selectedIndex = indexPath.row
        myTblView.reloadData()
    
    }
    
  3. 现在要切换 btn,只需在 cellForRow 中执行此操作。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Mycell
    
        :            
        cell.myBtn.isSelected = indexPath.row == selectedIndex
        :
    
        return cell
    }
    
  4. 现在在情节提要中,为选定状态和默认状态分配按钮图像。

【讨论】:

  • 它确实有效@dahiya_boy..但是如果选择了一个单元格,那么如果我再次点击它,我希望它被取消选择..
  • @asd2 在tableView:didDeselectRowAtIndexPath: 中,您必须执行selectedIndex = -1tableview.reloadData() OR 您可以在didselect 中添加一个额外条件以获得相同的功能。
  • 抱歉@dahiya_boy 回复晚了。我尝试了您的建议..但是没有调用didDeselectRow...。此链接中的第四个答案似乎是.. link 的原因。还不能在didselect 中提出额外条件...
  • @asd2 我从未使用过didDeselect,所以我没有更新。您需要在 didSelect 方法中添加条件。它是一个简单的切换。你只需要维护selectedIndex 条件并在最后重新加载tableView。
  • 很抱歉打扰@dahiya_boy..我确实尝试过..但无法提出解决方案...您能介意提出解决方案吗..?
【解决方案2】:
 // if you want to use didselect or diddeselect than :

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          if let cell = tableView.cellForRow(at: indexPath)as? tableViewCell{
          cell.mRadioimage.image = UIImage(named: "radioCheckedImage")

            }
           }
   func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
          if let cell = tableView.cellForRow(at: indexPath)as? tableViewCell{
          cell.mRadioimage.image = UIImage(named: "radioUnCheckedImage")
              }       
          }

        // don't forget to choose singleSelection
        // hope its work for you

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多