// if you want to use didselect or diddeselect than : func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath)as? tableViewCell{ cell.mRadioimage.image = UIImage(named: "radioCheckedImage") } } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath)as? tableViewCell{ cell.mRadioimage.image = UIImage(named: "radioUnCheckedImage") } } // don't forget to choose singleSelection // hope its work for you
首先,您不需要按钮操作。按照以下步骤轻松进行完美编码:
在控制器中取一个var var selectedIndex = -1 // -1如果没有选择任何索引,如果你想默认选择任何选项,那么相应地改变它。
var selectedIndex = -1
使用 tableView 委托方法 didSelectRowAt indexPath
tableView
didSelectRowAt indexPath
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ selectedIndex = indexPath.row myTblView.reloadData() }
现在切换btn只是这样做 cellForRow 。
cellForRow
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Mycell : cell.myBtn.isSelected = indexPath.row == selectedIndex : return cell }
现在在故事板中,为选定和默认状态分配按钮图像。