1月15
tableView的cell創建方式
先從緩存池中,如果緩存池中沒有可循環利用的cell,先去中找到合適的cell
,如果storyboard沒有的話,再從我們寫的代碼創建
偏好設置
● 很多iOS應用都支持偏好設置,比如保存用戶名、密碼、字體⼤小等設置,iOS提供了一套標準的解決方案來爲應用加入偏好設置功能
● 每個應⽤都有個NSUserDefaults實例,通過它來存取偏好設置
● ⽐如,保存⽤戶名、字體大小、是否自動登錄。
// 存儲偏好設置
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.accountField.text forKey:AccountKey];
[defaults setObject:self.pwdField.text forKey:PwdKey];
[defaults setBool:self.rmbPwdSwitch.isOn forKey:RmbPwdKey];
[defaults setBool:self.autoLoginSwitch.isOn forKey:AutoLoginKey];
[defaults synchronize];
// 讀取上次的偏好設置
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.accountField.text = [defaults objectForKey:AccountKey];
self.rmbPwdSwitch.on = [defaults boolForKey:RmbPwdKey];
self.autoLoginSwitch.on = [defaults boolForKey:AutoLoginKey];
NSKeyedArchiver
- (instancetype)initWithCoder:(NSCoder *)coder
{
if (self = [super init]) {
self.name = [coder decodeObjectForKey:@"name"];
self.phone = [coder decodeObjectForKey:@"phone"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeObject:self.phone forKey:@"phone"];
}
要歸檔到Documents目錄下,下面方法獲得路徑Documents,一般寫在宏中方便調用
#define ContactsFilepath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"contacts.data"]
恢復(解碼)對象
// 1.從文件中讀取聯繫人數據
- (NSMutableArray *)contacts
{
if (_contacts == nil) {
// 1.從文件中讀取聯繫人數據
_contacts = [NSKeyedUnarchiver unarchiveObjectWithFile:ContactsFilepath];
// 2.如果數組爲nil
if (_contacts == nil) { // 文件不存在
_contacts = [NSMutableArray array];
}
}
return _contacts;
}
歸檔一個對象
// 歸檔
[NSKeyedArchiver archiveRootObject:self.contacts toFile:ContactsFilepath];
取得tableView中選中的那一行
// 取得選中的那行
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
監聽文本框發出的通知
通知的具體介紹看PPT12
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.accountField];
prepareForSegue:sender:
/**
* 執行segue後,跳轉之前會調用這個方法
* 一般在這裏給下一個控制器傳遞數據
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// 1.取得目標控制器(聯繫人列表控制器)
UIViewController *contactVc = segue.destinationViewController;
// 2.設置標題
contactVc.title = [NSString stringWithFormat:@"%@的聯繫人列表", self.accountField.text];
}
performSegueWithIdentifier:sender:
滑動刪除的功能
#pragma mark - tableView的代理方法
/**
* 如果實現了這個方法,就自動實現了滑動刪除的功能
* 點擊了刪除按鈕就會調用
* 提交了一個編輯操作就會調用(操作:刪除\添加)
* @param editingStyle 編輯的行爲
* @param indexPath 操作的行號
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) { // 提交的是刪除操作
// 1.刪除模型數據
[self.contacts removeObjectAtIndex:indexPath.row];
// 2.刷新表格
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
// 局部刷新某些行(使用前提:模型數據的行數不變)
// [self.tableView reloadRowsAtIndexPaths:<#(nonnull NSArray<NSIndexPath *> *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]
// 3.歸檔
[NSKeyedArchiver archiveRootObject:self.contacts toFile:ContactsFilepath];
}
}
叫出鍵盤
[self.phoneField becomeFirstResponder];
結構體轉value對象
value對象轉結構體
CGPoint pos = [pathPoints[i] CGPointValue];