objective-c - 带有 UIButton 单选按钮的 UICollectionViewCell - 一次选择一个单选按钮
问题描述
我有 UICollectionView 和 UIButton 作为 UICollectionViewCell.xib 中的单选按钮。
我需要一次选择一个单选按钮,如果按下下一个,则应取消选择上一个。我会很感激帮助。
//Loading radio button when view loads
-(void)setRadioButton{
if (!self.product.isSelectedCell) {
[self.radioBtn setBackgroundImage:[UIImage imageNamed:@"radio-button"] forState:UIControlStateNormal];
}
else
{
[self.radioBtn setBackgroundImage:[UIImage imageNamed:@"radio-button-select"] forState:UIControlStateNormal];
}
}
//Action for radio button
- (IBAction)radioBtnAction:(UIButton*)sender {
if([self.radioBtn isSelected]==YES)
{
[self.radioBtn setBackgroundImage:[UIImage imageNamed:@"radio-button-select"] forState:UIControlStateNormal];
}
else{
//always calls else
[self.radioBtn setBackgroundImage:[UIImage imageNamed:@"radio-button"] forState:UIControlStateNormal];
}
}
解决方案
Use this code in cellForRowAtIndexPath
UIButton *radioButton = (UIButton *) [cell.contentView viewWithTag:kRadioButtonTag]; // Radio button
[radioButton setImage:[UIImage imageNamed:@"radio_unselected"] forState:UIControlStateNormal];
[radioButton setImage:[UIImage imageNamed:@"RadioSelected"] forState:UIControlStateSelected];
[radioButton addTarget:self action:@selector(radioButtonPressed:) forControlEvents:UIControlEventTouchDown];
if(indexPath == selectedIndexPath)
[radioButton setImage:[UIImage imageNamed:@"RadioSelected"] forState:UIControlStateSelected];
else
[radioButton setImage:[UIImage imageNamed:@"radio_unselected"] forState:UIControlStateNormal];
Radio button taped from table cell and get index path of that button
Take on class Lebel NSIndexPath variable and store selected index path.
- (void)radioButtonPressed:(UIButton *)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableView];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil) {
selectedIndexPath = indexPath
[tableView reloadData];
}
}
希望这会有所帮助
推荐阅读
- batch-file - 如何使 .bat 文件检查程序并在未打开时重新启动,如果程序已运行 x 时间,则还重新启动程序?
- javascript - 动态更改 Dropzone url
- laravel - 带有 Laravel 和数据库的树视图
- rxjs - 可观察性以及如何控制结果速度
- ionic-framework - 离子等待GIF结束,然后改变视图
- audio - 为什么 Windows 文件属性显示 48 kHz、16 位编码的比特率:1152 kBit/s?
- javascript - 使用 PHP 数组和 JS 将值填充到下拉列表中
- android - 如何检查我们的项目中是否存在反射api-Android工作室
- sonarqube - 用于 MSBuild 的 SonarQube 未报告质量问题
- javascript - 在 d3 错误中绘制一条线