Swift tableview cell radio button implementation

时间:2018-09-18 20:09:43

标签: ios swift uitableview

I need to place a radio button in tableview custom cell. 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
    }

2 个答案:

答案 0 :(得分:2)

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!!

  • 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

enter image description here

  • Cool Tip # 1: Click on and select the cell's image view, and then set it's highlighted image to be your checked state. When the cell is highlighted or selected, the image view within will change to show its highlighted state.

enter image description here

  • Cool Tip # 2: Next, drag a UIView into the cell's content view, behind the text label. 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. 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. 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.

enter image description here

  • 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!

答案 1 :(得分:1)

在您的TableViewCell类中,为什么不创建数据源元素并为其覆盖didSet。同样在您的UITableView数据源中,我推荐的数组不只是String

我还没有编译下面的内容,所以这只是一个主意。

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)
            }
        }
    }
}

在视图控制器中,您当然必须在点击行时设置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]
}

对于您的Animal可能是这样的:

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