IOS implementation simple memorandum Program-TaskList
In real life, we often forget some things because there are too many and too many things. This application is a list of tasks developed based on such user pain points. Similar to the memo program provided by iOS. You can record some information at any time and save it in real time. When the program is re-opened, all the information you saved will be displayed. The Demo I have uploaded https://github.com/chenyufeng1991/iOS-TaskList.
This application mainly implements the use of iOS UITableView and CoreData for storage. Similar to ListView in Android, TableView can display information in the list and automatically increase the number of rows. CoreData is iOS's encapsulation of SQLite embedded databases, allowing you to conveniently add, delete, modify, and query data. We can implement the following through code:
(1) Because I need to Use CoreData to store data, I need to check the Use CoreData check box when creating a project at the beginning. The project name is TaskList.
(2) After creating a project, open the TaskList. xcdatamodeld file to design the data structure of CoreData. Click the Add Entity button below to Add an instance-Task. Then add a property taskname of the String type to the right, because for each task, we only store its task name.
.
(3) Add a task name input box, an Add button, and a TableView on the page. After code binding using segue, implement the following in 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) Comment * taskArray; @ property (strong, nonatomic) NSArray * arr; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // load database files when the application starts; optional * 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 // each section has several cells;-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. taskArray. count;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView progress: @ MyCell forIndexPath: indexPath]; cell. textLabel. text = [self. taskArray objectAtIndex: indexPath. row]; return cell ;}# pragma mark-click Add button-(IBAction) addTaskButtonClick :( id) sender {NSString * inputStr = [[NSMutableString alloc] initWithFormat: @ % @, self. inputTextField. text]; inputStr = [inputStr stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]; // if the input is null, no value is added. if ([inputStr isEqual: @]) {UIAlertController * alert = [UIAlertController blank: @ prompt message: @ the input content cannot be blank preferredStyle: blank]; [alert addAction: [UIAlertAction actionWithTitle: @ confirm style: UIAlertActionStyleDefault handler: nil]; [self presentViewController: alert animated: true completion: nil];} else {// each time you click the "OK" button, the data is stored in CoreData; [self saveToCoreData: inputStr]; // store a text in the taskArray array. [self. taskArray insertObject: self. inputTextField. text atIndex: self. taskArray. count]; [self. taskTableView reloadData]; // clear the input box; self. inputTextField. text = nil; // click OK and the keyboard disappears. [self. inputTextField resignFirstResponder] ;}# pragma mark-save data to CoreData;-(void) saveToCoreData :( NSString *) taskName {NSManagedObjectContext * context = [(AppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext]; NSManagedObject * row = [NSEntityDescription failed: @ Task failed: context]; [row setValue: taskName forKey: @ taskname]; [context save: nil]; NSLog (@ saved to database);} # Hide the soft keyboard when rolling TableView;-(void) scrollViewDidScroll :( UIScrollView *) scrollView {[self. inputTextField resignFirstResponder];} @ end
The final implementation result is as follows:
.
.