iOS實現簡單備忘錄程式——TaskList
在實際生活中,由於事情太多太雜,我們往往會忘記一些事情。該應用就是基於這樣的使用者痛點而開發的工作清單。類似iOS內建的備忘錄程式。你可以隨時記錄一些資訊,並可以即時的進行儲存。在程式重新開啟時會顯示你儲存的所有資訊。該Demo我已經上傳 https://github.com/chenyufeng1991/iOS-TaskList 。
該應用主要實現的技術就是iOS控制項UITableView的使用和CoreData進行儲存。TableView類似Android中的ListView,可以列表顯示資訊,並自動增加行數。而CoreData是iOS對SQLite嵌入式資料庫的封裝,可以極為方便的進行資料的增刪改查。我們通過代碼來實現一下:
(1)由於我需要使用CoreData來儲存資料,所以在一開始建立項目的時候需要勾選Use CoreData那個複選框。項目名稱為TaskList.
(2)在建立完項目後,開啟TaskList.xcdatamodeld檔案,進行CoreData的資料結構設計。點擊下方的Add Entity按鈕,增加一個執行個體——Task。然後在右側增加一個屬性taskname,類型為String.因為在對於每一個任務,我們暫時只儲存它的任務名稱即可。
。
(3)在介面中增加一個任務名稱輸入框,一個增加按鈕,一個TableView即可。在使用segue進行代碼綁定後,在ViewController中實現如下:
#import ViewController.h#import AppDelegate.h#import @interface ViewController ()@property (weak, nonatomic) IBOutlet UITextField *inputTextField;@property (weak, nonatomic) IBOutlet UITableView *taskTableView;@property(strong,nonatomic) NSMutableArray *taskArray;@property(strong,nonatomic) NSArray *arr;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //應用啟動的時候載入資料庫檔案; NSManagedObjectContext *context = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext]; NSFetchRequest *fetchData = [[NSFetchRequest alloc] initWithEntityName:@Task]; self.arr = [context executeFetchRequest:fetchData error:nil]; self.taskArray = [[NSMutableArray alloc] initWithArray:[self.arr valueForKey:@taskname]];}#pragma mark - UITableViewDataSource//每一個section有幾個cell;- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.taskArray.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@MyCell forIndexPath:indexPath]; cell.textLabel.text = [self.taskArray objectAtIndex:indexPath.row]; return cell;}#pragma mark - 點擊增加按鈕- (IBAction)addTaskButtonClick:(id)sender { NSString *inputStr = [[NSMutableString alloc] initWithFormat:@%@,self.inputTextField.text]; inputStr = [inputStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //判斷如果輸入的為空白,則不添加; if ([inputStr isEqual: @]) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@提示 message:@輸入的內容不可為空 preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@確定 style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alert animated:true completion:nil]; } else { //每點擊一次“確定”按鈕後,就把該資料存放區到CoreData中; [self saveToCoreData:inputStr]; //把一個文本儲存到taskArray數組中; [self.taskArray insertObject:self.inputTextField.text atIndex:self.taskArray.count]; [self.taskTableView reloadData]; //清空輸入框; self.inputTextField.text = nil; //點擊確定後消失軟鍵盤; [self.inputTextField resignFirstResponder]; } }#pragma mark - 儲存資料到CoreData;- (void) saveToCoreData:(NSString *)taskName{ NSManagedObjectContext *context = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext]; NSManagedObject *row = [NSEntityDescription insertNewObjectForEntityForName:@Task inManagedObjectContext:context]; [row setValue:taskName forKey:@taskname]; [context save:nil]; NSLog(@已儲存到資料庫);}#pragma mark - UIScrollViewDelegate//滾動TableView的時候隱藏軟鍵盤;- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ [self.inputTextField resignFirstResponder]; }@end
最後的實現效果如下:
。
。