In real life, because things are too much and too miscellaneous, we tend to forget some things. The app is a list of tasks developed based on such user pain points. A memo program similar to the iOS comes with. You can record some information at any time, and you can save it in real time. When the program is reopened, all information you have saved is displayed. The demo I have uploaded https://github.com/chenyufeng1991/iOS-TaskList.
The main technology implemented by the app is the use of iOS control UITableView and CoreData storage. TableView like a ListView in Android, you can list information and automatically increase the number of rows. And CoreData is iOS to the SQLite embedded database encapsulation, can be very convenient to do the data deletion and modification. We do this by code:
(1) Since I need to use CoreData to store data, I need to tick the check box for using CoreData when I first create a project. The project name is tasklist.
(2) After the project is created, open the Tasklist.xcdatamodeld file for COREDATA data structure design. Click the Add Entity button below to add an instance of--task. Then add an attribute taskname to the right, and the type is string. Because for each task, we only store its task name for the time being.
。
(3) Add a Task Name input box in the interface, an Add button, a tableview. After using segue for code binding, the following is implemented in Viewcontroller:
#import "ViewController.h" #import "AppDelegate.h" #import <CoreData/CoreData.h> @interface Viewcontroller () <UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate> @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]; Load the database file when the app starts; 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//Each section has several 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-click the Add button-(ibaction) Addtaskbuttonclick: (ID) Sender {nsstring *inputstr = [[Nsmutablestring alloc] Initwith format:@ "%@", Self.inputTextField.text]; INPUTSTR = [Inputstr stringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset]]; Determine if the input is empty, not added if ([Inputstr isequal: @ "]) {Uialertcontroller *alert = [Uialertcontroller alertcontrollerwit htitle:@ "Prompt" message:@ "input content cannot be empty" preferredstyle:uialertcontrollerstylealert]; [Alert addaction:[uialertaction actionwithtitle:@ "OK" style:uialertactionstyledefault Handler:nil]]; [Self Presentviewcontroller:alert animated:true completion:nil]; } else {//per pointOnce the "OK" button is clicked, the data is stored in the coredata; [Self savetocoredata:inputstr]; Storing a text in a taskarray array; [Self.taskarray InsertObject:self.inputTextField.text AtIndex:self.taskArray.count]; [Self.tasktableview Reloaddata]; Empty the input box; self.inputTextField.text = nil; Click OK to disappear the soft keyboard; [Self.inputtextfield Resignfirstresponder]; }} #pragma mark-save data to coredata;-(void) Savetocoredata: (NSString *) taskname{nsmanagedobjectcontext *context = [(Ap pdelegate*) [[UIApplication Sharedapplication] delegate] Managedobjectcontext]; Nsmanagedobject *row = [nsentitydescription insertnewobjectforentityforname:@ "Task" Inmanagedobjectcontext:context] ; [Row Setvalue:taskname forkey:@ "TaskName"]; [Context Save:nil]; NSLog (@ "saved to Database");} #pragma mark-uiscrollviewdelegate//scroll tableview to hide the soft keyboard;-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{[ Self.inputtextfield Resignfirstresponder]; } @end
The final implementation results are as follows:
。
。
GitHub home: https://github.com/chenyufeng1991. Welcome everyone to visit!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS implementation Simple Memo program--tasklist