Ios 如何知道UISwitch的编号,即;关于;从一个角度来看?

Ios 如何知道UISwitch的编号,即;关于;从一个角度来看?,ios,objective-c,iphone,uitableview,uiswitch,Ios,Objective C,Iphone,Uitableview,Uiswitch,有没有办法知道在UITableView中处于“打开”状态的UISwitch的数量?我在一个UITableView中有一个多个UITableView单元格-每个都有一个处于“开”状态的UISwitch。我认为代码更像是: for ([mySwitch on] in tableView){ code goes here..... } 您必须根据UISwitch维护可变数组(NSMutableArray),当开关打开/关闭时,您必须在可变数组中维护该值(标志) 重新加载UITableView

有没有办法知道在UITableView中处于“打开”状态的UISwitch的数量?我在一个UITableView中有一个多个UITableView单元格-每个都有一个处于“开”状态的UISwitch。我认为代码更像是:

for ([mySwitch on] in tableView){
   code goes here.....
} 

您必须根据UISwitch维护可变数组(NSMutableArray),当开关打开/关闭时,您必须在可变数组中维护该值(标志)

重新加载UITableView时,使所有数组项都具有ON标志。当您将开关更改为off时,然后触发一个switch方法,并且根据indexpath.row,您必须在数组中的objectAtIndex处设置off标志


因此,该数组将为您提供开关打开或关闭时的所有值。

您必须根据UISwitch维护可变数组(NSMutableArray),当开关打开/关闭时,您必须在可变数组中维护该值(标志)

重新加载UITableView时,使所有数组项都具有ON标志。当您将开关更改为off时,然后触发一个switch方法,并且根据indexpath.row,您必须在数组中的objectAtIndex处设置off标志


因此,该阵列将为您提供开关打开或关闭的所有值。

您知道,ios开发使用的是MVC模式,您在视图中显示的内容或UI小部件的状态应该与您的视图模型绑定,就像您的情况一样,您可以创建一个视图模型,如
SwichViewModel
,它有一个BOOL属性
isSwitchOn
,加载表视图时,您可以根据其视图模型的
isSwitchOn
属性打开/关闭开关,打开开关的计数是
isSwitchOn
为是的视图模型的计数。下面是我的示例代码:

//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

您知道,ios开发使用的是MVC模式,您在视图中显示的内容或UI小部件的状态应该与您的视图模型绑定,在您的情况下,您可以创建一个视图模型,如
SwichViewModel
,当您加载表视图时,它具有BOOL属性
isSwitchOn
,根据其视图模型的
isSwitchOn
属性打开/关闭开关,打开开关的计数是
isSwitchOn
为是的视图模型的计数。下面是我的示例代码:

//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


您的tableview使用的是静态单元格还是动态单元格?答案会非常不同。我使用的是动态单元格。这里仍然需要一些帮助查看我的答案,希望它能帮助您添加链接-我很迷路。您的tableview使用的是静态单元格还是动态单元格?答案会非常不同。我使用动态单元格。这里仍然需要一些帮助查看我的答案,希望它能帮助您添加链接-我很迷路。你好,MudOnTire感谢它的工作-尽管我无法将开关调整为“关闭”,即使我已经在其上放置了一个静态值否。MyBookingObjects*booking1=[[MyBookingObjects alloc]initWithId:@“1”开始日期时间:@“开始日期”结束日期时间:@“结束日期”地点可用性:@“2”街道地址:@“SomeStreet”郊区:@“SomeSuburd”邮政编码:@“SomePostalCode”联系人号码:@“SomeContactNumber”儿童姓名:@“尚未保存儿童”。儿童销售:无附加信息:@“清洗餐具”档案图像:[UIImage图像名称:@“boy\u未选中”]IsSwitch:@(否)];正确的条目是什么isSwitchOn:[nsnumber numberWith Bool:0];或isSwitchOn:@(否)这两种语法是等效的。如果要使开关具有交互性,请使用delegation.hi MudOnTire感谢它的工作-尽管我无法将开关调整为“关闭”,即使我已经在其上放置了一个静态值NO.MyBookingObject*booking1=[[MyBookingObjects alloc]initWithId:@“1”开始日期:@“开始日期”结束日期:@“结束日期”地点可用性:@“2”街道地址:@“SomeStreet”郊区:@“SomeSuburd”postalCode:@“SomePostalCode”联系人号码:@“SomeContactNumber”孩子姓名:@“尚未保存孩子”。孩子已售出:无附加信息:@“清洗盘子”档案图像:[UIImage ImageName:@“boy_unselected”]isSwitchOn:@(否)];正确的条目是什么isSwitchOn:[nsnumber numberWith Bool:0];或isSwitchOn:@(否)。这两种语法是等效的。如果要使开关交互,请使用委派。