【发布时间】:2019-04-09 18:58:33
【问题描述】:
我的tableView 中有 3 个部分,我希望每个部分都像单选按钮一样进行单选。我的代码适用于单个部分,但不适用于多个部分。我没有为此使用任何自定义按钮,直接使用contentView imageView 设置图像和didSelectRowAt indexPath: 进行选择
我的代码是
cell?.imageView?.image = UIImage.init(named: "radio1")//Deselected state
cell?.imageView?.tag = indexPath.row
cell?.contentView.viewWithTag(8888)
if (selectedIndexPath != nil && selectedIndexPath?.section == 0 && selectedIndexPath?.row == indexPath.row) {
cell?.imageView?.image = UIImage.init(named: "radio2")//Selected state
} else {
cell?.imageView?.image = UIImage.init(named: "radio1")//Deselected state
}
if selectedIndexPath?.section == 1 {
}
if selectedIndexPath?.section == 2 {
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndexPath = indexPath//Save selected indexpath
print(selectedIndexPath)
tblView.reloadData()
上面的代码得到类似的结果
当我选择(Section1,Row0)时,自动(Section2,Row0)也被选中。我想根据部分单独选择行。
【问题讨论】:
-
你为什么不使用带状态的按钮?我对你的问题非常不清楚。
-
你能告诉这个你是怎么创建这个的吗
selectedIndexPath -
没有使用按钮,因为我们有 imageView 来显示图像和 didSelectrow 在索引路径功能供选择。
-
@Anbu.Karthik, var selectedIndexPath:IndexPath?全局创建
-
有什么问题.. 改变图像?或无法选择多部分..按钮也有图像属性..单选按钮..使用uIbutton bcz更方便。
标签: ios swift uitableview