WFCUGeneralSwitchTableViewCell.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // SwitchTableViewCell.m
  3. // WildFireChat
  4. //
  5. // Created by heavyrain lee on 27/12/2017.
  6. // Copyright © 2017 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUGeneralSwitchTableViewCell.h"
  9. #import "MBProgressHUD.h"
  10. @interface WFCUGeneralSwitchTableViewCell()
  11. @end
  12. @implementation WFCUGeneralSwitchTableViewCell
  13. - (void)awakeFromNib {
  14. [super awakeFromNib];
  15. // Initialization code
  16. }
  17. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  18. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  19. if(self) {
  20. self.valueSwitch = [[UISwitch alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 56, 8, 40, 40)];
  21. [self.contentView addSubview:self.valueSwitch];
  22. [self.valueSwitch addTarget:self action:@selector(onSwitch:) forControlEvents:UIControlEventValueChanged];
  23. }
  24. return self;
  25. }
  26. - (void)onSwitch:(id)sender {
  27. BOOL value = _valueSwitch.on;
  28. __weak typeof(self)ws = self;
  29. if (self.onSwitch) {
  30. self.onSwitch(value, self.type, ^(BOOL success) {
  31. if (success) {
  32. [ws.valueSwitch setOn:value];
  33. } else {
  34. [ws.valueSwitch setOn:!value];
  35. }
  36. });
  37. }
  38. }
  39. - (void)setOn:(BOOL)on {
  40. _on = on;
  41. [self.valueSwitch setOn:on];
  42. }
  43. @end