[英]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!! 这是一个很棒的解决方案,它使用需要零代码的情节
UITableView
在UITableView
创建单选按钮-并具有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 添加一个基本单元格,将图像设置为未选中的按钮图像,并确保选择样式为默认
UIView
into the cell's content view, behind the text label. 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. 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.