无法获得单一选择复选框


trpnest
2025-01-19 08:37:02 (2月前)
  1. 我制作了一个tableviewcell,里面有一个按钮。我连接了tableviewcell.swift类中按钮的IBAction。然后我做了一个代表,并在...中访问按钮触摸操作

3 条回复
  1. 0# google你他吗 | 2019-08-31 10-32




    1. // if you want to use didselect or diddeselect than :

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

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

    6.     // don't forget to choose singleSelection
    7.     // hope its work for you
    8. </code>

  2. 1# 啊这回彻底死了 | 2019-08-31 10-32



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





    1. 在控制器中取一个var

      var selectedIndex = -1

      // -1如果没有选择任何索引,如果你想默认选择任何选项,那么相应地改变它。





    2. 使用

      tableView

      委托方法

      didSelectRowAt indexPath




      1. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

      2. selectedIndex = indexPath.row
      3. myTblView.reloadData()
      4. }

      5.     </code>
      6.   </pre>
      7. </LI>
      8. <LI>
      9.   <P>
      10.     现在切换btn只是这样做
      11.      <code>
      12.       cellForRow
      13.     </code>
      14.     
      15.   </p>
      16.    <pre>
      17.     <code>
      18.       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      19. let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Mycell
      20. :            
      21. cell.myBtn.isSelected = indexPath.row == selectedIndex
      22. :
      23. return cell
      24. }

      25.     </code>
      26.   </pre>
      27. </LI>
      28. <LI>
      29.   <P>
      30.     现在在故事板中,为选定和默认状态分配按钮图像。
      31.   </p>
      32. </LI>
      33. </醇>











登录 后才能参与评论