如何在iOS中按钮单击时取消选择所有单选按钮uitableview单元格
我在UITableView中显示人名和NSMutableArray以及单元格中的单选按钮,我想知道的是如何使它在用户点击导航栏中的按钮可以将该按钮命名为clearAllButton,所有表格的内容都被选中(我的意思是所有的单选按钮都被选中),如何改变RadioButton使其在clearAllButton点击时取消选择全部(我想取消选择所有radioButtons当用户点击clearAllButton)。我是一个新的开发人员任何帮助将不胜感激! 我已经尝试clearAllButtonAction下面的代码,但它不工作。如何在iOS中按钮单击时取消选择所有单选按钮uitableview单元格
- (IBAction)clearAllBtnActn:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if([btnRadio isSelected]);
{
[btnRadio setSelected:NO];
}
}
首先需要一个基础数据源(例如MyItem
的NSArray
对象),从中可以驱动工作台视图细胞。
你MyItem
对象可能是这样的:
@interface MyItem()
@property (nonatomic, strong) NSString *title;
@property (nonatomic, readwrite) BOOL selected;
@end
在您的视图控制器创建数据源myItems
:
@interface MyViewController()
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *myItems;
@end
在您的视图控制器,创建你的细胞:
@implementation MyViewController
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyButtonTableViewCell *cell = [tableView [email protected]"ButtonCell" forIndexPath:indexPath];
MyItem *itemForCell = myItems[indexPath.row];
cell.titleLabel.text = itemForCell.title;
cell.radioButton.selected = itemForCell.selected;
return cell;
}
...
@end
然后你的“清晰”方法会看起来li关键字:
- (IBAction)clearAllBtnActn:(id)sender {
// Deselect all items
for (MyItem *item in self.myItems)
item.selected = NO;
[self.tableView reloadData];
}
嗨谢谢你的回应..你的意思是MyItem? – hani
表格中的每个单元格都显示有关'某事'(一个项目)的数据。实际的物品需要放在与桌子视图完全分开的模型中。您应该创建一个名为MyItem的类(或任何您想要的)来表示一个项目。然后创建一个数组来保存所有项目的实际数据。阅读MVC(https://en.wikipedia。org/wiki/Model-view-controller)和UITableView(https://developer.apple.com/reference/uikit/uitableview?language=objc) – norders
,但我没有采取任何课堂..那么将是什么答案? – hani
为此您必须携带一个数组。下面
NSMutableArray *arraySelectObject;
你在哪里得到的名称数组只是调用lines.Suppose你的名字阵列将返回5对象,然后,
arraySelectObject=[[NSMutableArray alloc]init];
for (int i=0, i<=name.count,i++)
{
[arraySelectObject addObject:@"0"];
}
如果您选择或不选择的单选按钮,检查按钮动作
-(IBAction)selectButton:(id)sender
{
if([arraySelectObject objectAtIndex:sender.tag]==true)
{
[arraySelectObject replaceObjectAtIndex:sender.tag withObject:@"0"];
}
else
{
[arraySelectObject replaceObjectAtIndex:sender.tag withObject:@"1"];
}
[tableView reloadData];
}
并在您的全部清除按钮方法
- (IBAction)clearAllBtnActn:(id)sender
{
for (int i=0, i<=name.count,i++)
{
[arraySelectObject addObject:@"0"];
}
[tableView reloadData];
}
而在的tableView cellForrow方法显示选择和取消选择单选按钮图像或任何代码
if([arraySelectObject objectAtIndex:sender.tag]==true)
{
//Set the image or your code of the select radio button /////
}
else
{
////////// Set the image or code of the Deselect radio button ///////
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
CustomCell *cell = [tableview cellForRowAtIndexPath:indexPath];
BOOL b = cell.check;
if(b)
{
cell.check = NO;
[cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-uncheck.png"] forState:UIControlStateNormal];
}
else{
cell.check = YES;
[cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-check.png"] forState:UIControlStateNormal];
}
}
表明我们的代码... –
什么你上面的代码happend? – Abha
请再次检查我的问题..#Karthik – hani