Reputation: 437
I have a UISwitch in UITableView,I want to pass the event as the following way,but the event is nil.If I have replaced the switch for the button, the event will exist
[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);
}
Upvotes: 1
Views: 68
Reputation: 34
[tableName indexPathForRowAtPoint: [sender convertPoint:CGPointZero toView: tableName]]
Used this worked fine.
Upvotes: 0
Reputation: 20804
This is an implementation of my own, a generic UITableViewCell for switch selection common tasks
#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
#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
EXAMPLE OF USE
- (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;
}
Upvotes: 0