// Click Event-(void) barbuttonitemclicked {// create data int randomname = arc4random () % 300 + 160000; nsarray * cityarray = @ [@ "Shanghai ", @ "Tianjin", @ "Beijing", @ "Zhengzhou", @ "Guangzhou"]; int randomcity = arc4random () % 5; nsstring * citystr = [cityarray objectatindex: randomcity]; nsdate * Date = [nsdate date]; nsdateformatter * formatter = [[nsdateformatter alloc] init]; [formatter setdateformat: @ "yyyy-mm-dd hh: mm: SS "]; nsstring * time = [formatter stringfromdate: date]; // create object classes * classes = [nsentitydescription insertnewobjectforentityforname: @" classes "inmanagedobjectcontext: Self. managedcontext]; // attribute classes. classessname = [nsstring stringwithformat: @ "BJs % d", randomname]; classes. classesscity = citystr; classes. classesstime = time; classes. classessimage = @ "12.jpg"; [_ managedcontext save: Nil]; [self. dataarray addobject: classes]; [self. tableview reloaddata];}
Delete object
# Pragma mark-set all cells to editable-(bool) tableview :( uitableview *) tableview caneditrowatindexpath :( nsindexpath *) indexpath {return yes;} # pragma mark-edit style-(uitableviewcelleditingstyle) tableview :( uitableview *) tableview editingstyleforrowatindexpath :( nsindexpath *) indexpath {return finished;} # pragma mark-complete and submit-(void) tableview :( uitableview *) tableview commiteditingstyle :( Preview) editingstyle forrowatindexpath :( nsindexpath *) indexpath {If (editingstyle = uitableviewcelleditingstyledelete) {Classes * model = self. dataarray [indexpath. row]; [self. managedcontext deleteobject: Model]; nserror * error = nil; [self. managedcontext save: & error]; If (error = nil) {[self. dataarray removeobject: Model]; [self. tableview deleterowsatindexpaths: @ [indexpath] withrowanimation: uitableviewrowanimationfade] ;}}
Randomly create click object