我正在尝试在ViewController中的UITableView中的自定义单元格中实现FlatUIKit,该单元格根据开关状态向NSUserDefaults返回值。作为一个常见的开关,我有方法' saveSwitchState'连接到valueChanged outlet。问题在于我想将状态传递给用户默认值并使用标签作为键,但该方法无法访问单元格和标签。我试过添加:
static NSString *CellIdentifier = @"Cell";
setupOneCell *cell = (setupOneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
到方法,但仍然没有运气。如果有一种更好的方法来保存交换机状态,那么使用valueChanged方法,我只需要所有的耳朵。下面是我的ViewController和Custom Cell。
ViewController.m:
#import "setup1ViewController.h"
#import "setup1TableView.h"
#import "setupOneCell.h"
#import "Social.h"
#import "FUISwitch.h"
#import "UIFont+FlatUI.h"
#import "UIColor+FlatUI.h"
@interface setup1ViewController ()
- (IBAction)saveSwitchState:(id)sender;
@end
@implementation setup1ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.oneTableView.dataSource = self;
self.oneTableView.delegate = self;
self.medias = [[NSMutableArray alloc] init];
Social *media = [[Social alloc] init];
media.socialMedia = @"Facebook";
[self.medias addObject:media];
media = [[Social alloc] init];
media.socialMedia = @"Twitter";
[self.medias addObject:media];
media = [[Social alloc] init];
media.socialMedia = @"Instagram";
[self.medias addObject:media];
media = [[Social alloc] init];
media.socialMedia = @"Tumblr";
[self.medias addObject:media];
media = [[Social alloc] init];
media.socialMedia = @"Gmail";
[self.medias addObject:media];
media = [[Social alloc] init];
media.socialMedia = @"Linked In";
[self.medias addObject:media];
media = [[Social alloc] init];
media.socialMedia = @"GitHub";
[self.medias addObject:media];
media = [[Social alloc] init];
media.socialMedia = @"You Tube";
[self.medias addObject:media];
media = [[Social alloc] init];
media.socialMedia = @"Vine";
[self.medias addObject:media];
media = [[Social alloc] init];
media.socialMedia = @"SoundCloud";
[self.medias addObject:media];
//self.setup1Table.editing = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.medias count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
setupOneCell *cell = (setupOneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.cellSwitch.onColor = [UIColor turquoiseColor];
cell.cellSwitch.offColor = [UIColor cloudsColor];
cell.cellSwitch.onBackgroundColor = [UIColor midnightBlueColor];
cell.cellSwitch.offBackgroundColor = [UIColor silverColor];
cell.cellSwitch.offLabel.font = [UIFont boldFlatFontOfSize:14];
cell.cellSwitch.onLabel.font = [UIFont boldFlatFontOfSize:14];
Social *media = (self.medias)[indexPath.row];
cell.socialName.text = media.socialMedia;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return;
}
- (IBAction)saveSwitchState:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([cell.cellSwitch isOn])
[defaults setBool:YES forKey:cellLabel];
else
[defaults setBool:NO forKey:cellLabel];
}
@end
setupOneCell.h:
#import <UIKit/UIKit.h>
#import "FUISwitch.h"
#import "UIFont+FlatUI.h"
#import "UIColor+FlatUI.h"
@interface setupOneCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *socialName;
@property (weak, nonatomic) IBOutlet FUISwitch *cellSwitch;
@end