如何知道一个UITableView中有"On"个UISwitch?
How to know the number of UISwitch that is "On" in a UITableView?
有没有办法知道 UITableView 中处于“ON”状态的 UISwitch 的数量?我在 UITableView 中有多个 UITableViewCell - 每个都有一个处于 "ON" 状态的 UISwitch。我认为代码更像是:
for ([mySwitch on] in tableView){
code goes here.....
}
您必须根据 UISwitch 维护可变数组 ( NSMutableArray ),当开关打开/关闭时,您必须在可变数组中维护该值 ( flag )。
当您重新加载 UITableView 时,使所有数组项都带有 ON 标志。当您将开关更改为关闭时,然后触发开关方法,并且根据 indexpath.row 您必须在数组中的 objectAtIndex 处关闭标志。
因此该数组将为您提供所有打开或关闭开关的值。
你知道,ios 开发正在使用 MVC 模式,你在视图中显示的内容或 UI 小部件的状态应该与你的视图模型绑定,就像你的情况一样,你可以创建一个像 SwichViewModel
这样的视图模型,它有一个 BOOL 属性 isSwitchOn
,当你加载你的 table 视图时,你可以根据它的视图打开 on/off 开关模型的 isSwitchOn
属性,打开开关的计数是 isSwitchOn
为 YES 的视图模型的计数。下面是我的示例代码:
//create the view model
@interface SwitchViewModel : NSObject
@property (assign, nonatomic) BOOL isSwitchOn;
@end
@interface TableViewController ()
@property (strong, nonatomic) NSArray *switchViewModels;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.switchViewModels = [self getData]; // you need to implement the getData method
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.switchViewModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SwitchViewModel *model = self.switchViewModels[indexPath.row];
SwitchTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
cell.switcher.on = model.isSwitchOn;
return cell;
}
- (NSInteger)getCountOfSwitchsOn{
NSArray *switchsOn = [self.switchViewModels filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isSwitchOn == %@", @(YES)]];
return switchsOn.count;
}
@end
有没有办法知道 UITableView 中处于“ON”状态的 UISwitch 的数量?我在 UITableView 中有多个 UITableViewCell - 每个都有一个处于 "ON" 状态的 UISwitch。我认为代码更像是:
for ([mySwitch on] in tableView){
code goes here.....
}
您必须根据 UISwitch 维护可变数组 ( NSMutableArray ),当开关打开/关闭时,您必须在可变数组中维护该值 ( flag )。
当您重新加载 UITableView 时,使所有数组项都带有 ON 标志。当您将开关更改为关闭时,然后触发开关方法,并且根据 indexpath.row 您必须在数组中的 objectAtIndex 处关闭标志。
因此该数组将为您提供所有打开或关闭开关的值。
你知道,ios 开发正在使用 MVC 模式,你在视图中显示的内容或 UI 小部件的状态应该与你的视图模型绑定,就像你的情况一样,你可以创建一个像 SwichViewModel
这样的视图模型,它有一个 BOOL 属性 isSwitchOn
,当你加载你的 table 视图时,你可以根据它的视图打开 on/off 开关模型的 isSwitchOn
属性,打开开关的计数是 isSwitchOn
为 YES 的视图模型的计数。下面是我的示例代码:
//create the view model
@interface SwitchViewModel : NSObject
@property (assign, nonatomic) BOOL isSwitchOn;
@end
@interface TableViewController ()
@property (strong, nonatomic) NSArray *switchViewModels;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.switchViewModels = [self getData]; // you need to implement the getData method
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.switchViewModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SwitchViewModel *model = self.switchViewModels[indexPath.row];
SwitchTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
cell.switcher.on = model.isSwitchOn;
return cell;
}
- (NSInteger)getCountOfSwitchsOn{
NSArray *switchsOn = [self.switchViewModels filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isSwitchOn == %@", @(YES)]];
return switchsOn.count;
}
@end