简体   繁体   中英

Swift tableview cell radio button implementation

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
    }

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

在此处输入图片说明

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

在此处输入图片说明

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

在此处输入图片说明

  • 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. also in your data source for the UITableView I would recommend an array of something more than just a 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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