스위치 상태에 따라 NSUserDefaults 값을 반환하는 내 ViewController 내에서 UITableView의 사용자 지정 셀에 FlatUIKit을 구현하려고합니다. 일반적인 스위치로서 valueChanged 콘센트에 연결된 메서드 'saveSwitchState'가 있습니다. 문제는 내가 상태를 사용자 기본값으로 전달하고 레이블을 키로 사용하지만 메서드가 셀과 레이블에 액세스 할 수 없다는 것입니다. 추가 시도 :메서드에서 표 셀 식별
static NSString *CellIdentifier = @"Cell";
setupOneCell *cell = (setupOneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
이 방법은 있지만 여전히 행운입니다. 스위치 상태를 저장하는 더 좋은 방법이 있다면 valueChanged 메소드를 사용하여 모든 귀가 들게됩니다. 아래 내 ViewController 및 사용자 지정 셀입니다.
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