@interface TableSetting : UITableViewController
{
NSArray *sections;
NSUserDefaults *setSize;
}
@property (nonatomic, strong) NSArray *sections;
- (void) initSections;
@end
viewDidAppear 重要! 重新載入Table新的設定值才能顯示NSUserdefaults 讀取
.m
@synthesize sections;
- (void)viewDidLoad
{
[super viewDidLoad];
[self initSections];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self initSections];
[self.tableView reloadData];
NSLog(@"viewWillDisappear- reloadData");
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.sections count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSDictionary *each = [[self.sections objectAtIndex: indexPath.section] objectAtIndex: indexPath.row];
cell.textLabel.text = [each objectForKey: @"title"];
cell.detailTextLabel.text = [each objectForKey: @"value"];
return cell;
}
#pragma mark -
#pragma mark custom methods
- (void) initSections
{
// Get the data
setSize = [NSUserDefaults standardUserDefaults];
NSString *geSetSize = [setSize objectForKey:@"setSize"];
NSMutableDictionary *fontSize = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"Font Size", @"title",
geSetSize, @"value", nil];
NSLog(@"initSections fontSize:%@",fontSize);
NSArray *conversation = [[NSArray alloc] initWithObjects: fontSize, nil];
NSArray *data = [[NSArray alloc] initWithObjects: conversation, nil];
self.sections = data;
}
.h
@interface TableSettingOptions : UITableViewController{
NSArray *data;
NSUserDefaults *setSize;
}
@property (nonatomic, strong) NSArray *data;
@property (nonatomic, strong) NSUserDefaults *setSize;
- (void) initData;
@end
.m NSUserdefaults 寫入
@synthesize data;
@synthesize setSize;
- (void)viewDidLoad
{
[super viewDidLoad];
[self initData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSDictionary *each = [self.data objectAtIndex: indexPath.row];
cell.textLabel.text = [each objectForKey: @"title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize: [[each objectForKey: @"value"] integerValue]];
cell.detailTextLabel.text = [each objectForKey: @"value"];
cell.detailTextLabel.font = [UIFont systemFontOfSize: [[each objectForKey: @"value"] integerValue]];
cell.accessoryView = nil;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
setSize = [NSUserDefaults standardUserDefaults];
//get selected cell.detailTextLabel.text
NSDictionary *each = [self.data objectAtIndex: indexPath.row];
NSString *fontSize= [each objectForKey: @"value"];
//store data
[setSize setObject:fontSize forKey:@"setSize"];
}
#pragma mark -
#pragma mark custom methods
- (void) initData
{
NSMutableDictionary *small = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"Small", @"title",
@"5", @"value", nil];
NSMutableDictionary *medium = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"Medium", @"title",
@"10", @"value",nil];
NSMutableDictionary *large = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"Large", @"title",
@"15", @"value", nil];
NSArray *_data = [[NSArray alloc] initWithObjects: small, medium, large, nil];
self.data = _data;
}
不拉線的UI接法 注意identifier
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
switch (indexPath.section) {
case 0:
[self loadFontSetting];
return;
}
}
- (void) loadFontSetting
{
FontSetting *fontSettingpage = [self.storyboard instantiateViewControllerWithIdentifier:@"identifierFontSetting"];
[self.navigationController pushViewController:fontSettingpage animated: YES];
}
鎖定不移動Table
- (void)viewDidLoad
{
self.tableView.scrollEnabled = NO; //關閉不要被scroll影響
}
延伸閱讀:
NSUserDefaults Save/Read/Clear 儲存/讀取/清除 速記 NSUserDefaults integer
如何获取应用程序推出 iPhone 的次数的计数
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
NSLog(@"Unit didSelectRowAtIndexPath: %d",indexPath.row);
//int currentUnitID = indexPath.row;
NSUserDefaults * recordUnitID = [NSUserDefaults standardUserDefaults];
int UnitID = indexPath.row+1;
//store data
[recordUnitID setInteger:UnitID forKey:@"unitID"];
//get data
NSInteger value = [recordUnitID integerForKey:@"unitID"];
NSLog(@"NSUserDefaults unit didSelectRowAtIndexPath: %d",value);
}


沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。