2014-06-22 2 views
0

스위치 상태에 따라 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 

답변

1

기본 아이디어는 휴대 다음, 스위치 값의 변화 청취를 통해 뷰 컨트롤러에 해당 변경 사항을보고하는 것입니다 대의원 호출. 그런 다음보기 컨트롤러에서 해당 셀과 연결된 데이터를 찾아서 NSUserDefaults을 수정하는 데 사용할 수 있습니다.

이 수행하는 방법 : 셀의 설정 코드 내부

을 대신 뷰 컨트롤러가 대상이 될 필요없이 스위치의 대상으로 셀을 추가해야합니다. (셀의 -awakeFromNib하지만 그것도 -configureWithSocialMedia: 방법의 일종으로 일할 수있는가. 아마에)

- (void)awakeFromNib { 
    [self.cellSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
} 

- (void)switchValueChanged:(id)sender { 
    [self.delegate setupOneCell:self witchValueDidChange:[self.cellSwitch isOn]]; 
} 

이 그런 다음 셀과 스위치 값을 전달 -setupOneCell:switchValueDidChange: 같은 방법으로 프로토콜을 만들 수 있습니다. 이 프로토콜을 따르는 델리게이트로 셀을 만듭니다.

코드는 프로토콜에 대해 다음과 같이 될 것이다 :

@protocol SetupOneCellDelegate; 

@interface setupOneCell : UITableViewCell 

@property (weak, nonatomic) IBOutlet UILabel *socialName; 
@property (weak, nonatomic) IBOutlet FUISwitch *cellSwitch; 

@property (weak, nonatomic) id<SetupOneCellDelegate> delegate; 

@end 


@protocol SetupOneCellDelegate <NSObject> 

- (void)setupOneCell:(setupOneCell *)cell switchValueDidChange:(BOOL)switchValue; 

@end 

지금보기 컨트롤러 셀의 스위치 값의 변화로부터 콜백을 얻을 수있는 모든 집합입니다. 당신이 당신의 셀을 구성 할 때 대리인으로 뷰 컨트롤러를 설정해야합니다 :

- (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; 

    cell.delegate = self; 

    return cell; 
} 

그리고 마지막으로 대리자 메서드를 구현 :

- (void)setupOneCell:(setupOneCell *)cell switchValueDidChange:(BOOL)switchValue { 
    NSIndexPath *indexPath = [self.oneTableView indexPathForCell:cell]; 
    NSString *label = [self.medias[indexPath.row] socialMedia]; 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setBool:switchValue forKey:label]; 
}