【问题标题】:how to create custom radio button on ui table view cell in swift如何在swift中的ui表视图单元格上创建自定义单选按钮
【发布时间】:2018-02-20 01:11:24
【问题描述】:

我需要在 Swift 的表格视图中创建自定义单选按钮,这将是 1 个单元格中的 2 个以上的按钮。它只允许在一个单元格中一次选择 1 个按钮。

现在我已经创建了多个按钮被选中的按钮。

【问题讨论】:

  • 到目前为止你尝试过什么?显示一些代码,说明您尝试获得的具体反馈,说明您可能出错的地方。

标签: ios swift radio-button


【解决方案1】:

首先你必须使所有按钮背景清晰

func clearButtons() {
 button1.backgroundColor = .clear
 button2.backgroundColor = .clear
 button3.backgroundColor = .clear
 button4.backgroundColor = .clear
 button5.backgroundColor = .clear
 }

viewDidLoadviewWillAppear 中调用这个函数 还要制作Ib-Action of all buttons 并在每个按钮上调用此方法 Tapped IBAction forExample 如果按下按钮1,您必须这样做

 @IBAction func Button1Tapped(_ sender: Any) {
    clearButtons()
 button1.backgroundColor = .white
}

您必须在所有按钮操作中执行相同的操作并更改按下的按钮名称。

希望对您有所帮助....

【讨论】:

    【解决方案2】:

    您可以创建一个包含 collectionView(作为组件)的单元格,将其设置为单选并覆盖 UICollectionViewCell 的 selected 方法以更改颜色和内容...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多