/ / Чому UISwitch не може пройти подію? - ios, uibutton, uiswitch

Чому UISwitch не може пройти подію? - ios, uibutton, uiswitch

У мене є UISwitch в UITableView, я хочу передати подію наступним чином, але подія є нульовою. Якщо я замінив перемикач на кнопку, подія буде існувати

[switch addTarget:self action:@selector(switchTaped:event:) forControlEvents:UIControlEventValueChanged];

-(void)switchTaped:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:_tableView];
NSIndexPath *indexPath= [_tableView
indexPathForRowAtPoint:currentTouchPosition];
NSLog(@"%ld",indexPath.row);
}

Відповіді:

0 для відповіді № 1

Це власна реалізація, загальна UITableViewCell для вибору комутаторів загальних завдань

ФАЙЛ .h

#import <UIKit/UIKit.h>

@interface SPFGenericBoolDataTableViewCell : UITableViewCell
{
IBOutlet UILabel *dataTitleLabel;
IBOutlet UISwitch *swDataValue;
}

@property (strong, nonatomic) UILabel *dataTitleLabel;
@property (strong, nonatomic) UISwitch *swDataValue;

-(void)setupWithTitle:(NSString*)title
dataValue:(BOOL)dataValue
indexpath:(NSIndexPath*)indexPath
andDataUpdateCallback:(void(^)(BOOL newValue,NSIndexPath*indexpath)) callback;

@end

ФАЙЛ .m

#import "SPFGenericBoolDataTableViewCell.h"

@interface SPFGenericBoolDataTableViewCell()
@property (strong) void(^dataWasUpdatedBlock)(BOOL newValue,NSIndexPath*indexpath);
@property (strong) NSIndexPath* currIndexPath;
@end

@implementation SPFGenericBoolDataTableViewCell

@synthesize swDataValue;
@synthesize dataTitleLabel;

#pragma mark -
#pragma mark NSCoding

- (void)encodeWithCoder:(NSCoder*)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:self.dataTitleLabel forKey:@"dataTitleLabel"];
[coder encodeObject:self.swDataValue forKey:@"swDataValue"];
}

- (instancetype)initWithCoder:(NSCoder*)decoder
{
if(self = [super initWithCoder:decoder]){
self.dataTitleLabel = [decoder decodeObjectForKey:@"dataTitleLabel"];
self.swDataValue = [decoder decodeObjectForKey:@"swDataValue"];
}
return self;
}

-(void)setupWithTitle:(NSString*)title
dataValue:(BOOL)dataValue
indexpath:(NSIndexPath*)indexPath
andDataUpdateCallback:(void(^)(BOOL newValue,NSIndexPath*indexpath)) callback
{
self.dataTitleLabel.text = title;
[self.swDataValue setOn:dataValue];
self.dataWasUpdatedBlock = callback;
self.currIndexPath = indexPath;
[self.swDataValue removeTarget:self action:@selector(valueChangedAction:) forControlEvents:UIControlEventValueChanged];
[self.swDataValue addTarget:self action:@selector(valueChangedAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (void)valueChangedAction:(id)sender {
if(self.dataWasUpdatedBlock != nil)
self.dataWasUpdatedBlock(self.swDataValue.on,self.currIndexPath);
}

@end

ПРИКЛАД ВИКОРИСТАННЯ

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


SPFGenericBoolDataTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SPFGenericBoolDataTableViewCell" forIndexPath:indexPath];

[cell setupWithTitle:@"AnyTitle" dataValue:currValue indexpath:indexPath andDataUpdateCallback:^(BOOL newValue, NSIndexPath *indexpath) {
NSLog(@"%@",indexPath);
}];

return cell;
}