简体   繁体   English

Swift Tableview Cell单选按钮实现

[英]Swift tableview cell radio button implementation

I need to place a radio button in tableview custom cell. 我需要在tableview自定义单元格中放置一个单选按钮。 whenever user clicks the tableview cell or button then radio button needs to work. 每当用户单击表格视图单元格或按钮时,单选按钮都需要工作。 I tried by using below code but didn't execute well. 我尝试使用下面的代码,但执行效果不佳。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:TableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TableViewCell

        cell.country.text = self.animals[indexPath.row]

        cell.selectionStyle = UITableViewCellSelectionStyle.none;

        if selectedRows.contains(indexPath)
        {
            cell.radioButton.setImage(UIImage(named:"check.png"), for: .normal)
        }
        else
        {
            cell.radioButton.setImage(UIImage(named:"uncheck.png"), for: .normal)
        }

        return cell
    }

Here's a great solution for creating radio buttons in a UITableView using a storyboard that requires zero code - and has 2 great Cool Tips!! 这是一个很棒的解决方案,它使用需要代码的情节UITableViewUITableView创建单选按钮-并具有2个很棒的酷提示!!

  • Make sure your table view is set to Single Selection , and to use Static cells . 确保将表视图设置为“ 单选” ,并使用“ 静态”单元格

  • Add a Basic cell, set the image to be your unchecked button image, and make sure the selection style is Default 添加一个基本单元格,将图像设置为未选中的按钮图像,并确保选择样式为默认

在此处输入图片说明

  • Cool Tip # 1: Click on and select the cell's image view, and then set it's highlighted image to be your checked state. 酷提示#1:单击并选择单元格的图像视图,然后将其突出显示的图像设置为选中状态。 When the cell is highlighted or selected, the image view within will change to show its highlighted state. 突出显示或选择单元格后,其中的图像视图将更改以显示其突出显示的状态。

在此处输入图片说明

  • Cool Tip # 2: Next, drag a UIView into the cell's content view, behind the text label. 酷提示#2:接下来,将UIView拖到单元格的内容视图中的文本标签后面。 As you're using a basic cell, you won't be able to drop it directly into the cell, you'll need to drag it into onto the Document Outline on the left instead. 在使用基本单元格时,您将无法直接将其拖放到该单元格中,而需要将其拖动到左侧的Document Outline中。 Then hook this up to the cell's selected background view outlet. 然后将其连接到单元格的选定背景视图插座。 When a cell is selected (or highlighted), this view will be displayed in the background. 选择一个单元格(或突出显示一个单元格)时,此视图将在后台显示。 In this case, we're going to use it to prevent the grey background appearing, so set its colour to Clear . 在这种情况下,我们将使用它来防止出现灰色背景,因此将其颜色设置为Clear Note that it doesn't matter what size the view is, and there's no need to set any constraints - it's automatically sized to match the cell at runtime. 请注意,视图的大小无关紧要,并且无需设置任何约束-它会在运行时自动调整大小以匹配单元格。

在此处输入图片说明

  • Finally, duplicate this cell and change the text for each of your radio button options. 最后,复制此单元格并更改每个单选按钮选项的文本。 Build and run, and you have code-free radio buttons! 编译并运行,您将获得免代码单选按钮!

In your TableViewCell class why don't you create a data source element and override the didSet for it. TableViewCell类中,为什么不创建数据源元素并覆盖它的didSet also in your data source for the UITableView I would recommend an array of something more than just a String . 同样在您的UITableView数据源中,我建议使用的数组不仅仅是String

I haven't compiled the below so this is just an idea. 我还没有编译下面的内容,所以这只是一个想法。

import UIKit

class TableViewCell : UITableViewCell {
    var data: Animal? {
        didSet {
            self.country.text = data.description
            if (data.isSelected) {
                self.radioButton.setImage(UIImage(named:"check.png"), for: .normal)
            } else {
                self.radioButton.setImage(UIImage(named:"uncheck.png"), for: .normal)
            }
        }
    }
}

in your view controller you will of course have to set the isSelected property whenever a row is tapped. 当然,在视图控制器中,每当点击一行时,您都必须设置isSelected属性。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var animal = self.animals[indexPath.row]
    animal.isSelected = !animal.isSelected
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:TableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TableViewCell

    cell.data = self.animals[indexPath.row]
}

and for your Animal maybe something like this: 对于您的Animal可能是这样的:

struct Animal {
    var description: String
    var isSelected: Bool
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM