我在表格视图中有多个部分,每行都有一个开关。在每个部分 当用户点击保存按钮时,我想知道哪些开关处于“ON”或“OFF”状态。如何为交换机分配标签?
答案 0 :(得分:0)
如果您的问题是为不同部分中的每个switch
提供唯一标记,那么您可以这样做:
为每个部分分配一个与First Section 1001
类似的起始标记号,因此第一个单元格switch
将包含switch.tag = 1001+indexPath.row
标记并继续。
对于Second Section initial tag will be 2002
,第二个单元格switch
将包含switch.tag = 2001+indexPath.row
标记并继续。
因此,这将解决您的标记问题。
答案 1 :(得分:0)
为您的单元格实施委托:
@protocol SampleTableViewCellDelegate <NSObject>
- (void)sampleCellDidSelectSwitch:(UISwitch*)sender withIndexPath:(NSIndexPath*)indexPath;
@end
@interface SampleTableViewCell : UITableViewCell
@property (nonatomic, strong) NSIndexPath *cellIndexPath;
@property (nonatomic, assign) id <SampleTableViewCellDelegate> cellDelegate;
@end
在你的Cell.m中:
@implementation SampleTableViewCell
- (IBAction)switchValueChanged:(UISwitch *)sender {
if (self.cellDelegate) {
[self.cellDelegate sampleCellDidSelectSwitch:sender withIndexPath:self.cellIndexPath];
}
}
@end
在ViewController.m中:
#import "SampleTableViewCell.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate, SampleTableViewCellDelegate>
@end
@implementation ViewController
#pragma mark UITableViewDataSource methods
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"sampleCell";
SampleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[SampleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.cellDelegate = self;
cell.cellIndexPath = indexPath;
return cell;
}
#pragma mark - SampleTableViewCellDelegate methods
- (void)sampleCellDidSelectSwitch:(UISwitch*)sender withIndexPath:(NSIndexPath *)indexPath {
if (sender.on) {
}
}
@end