iphone - Scroll down uitableview to specific date -
i know if have uitable, title of each table items date , time. may know how code if want table auto scroll down cell specific date e.g. today's date? should code in viewdidload method?
here code table,
@interface picturelistmaintable : uitableviewcontroller{ iboutlet uibutton*scroll; } @property (strong, nonatomic) nsmanagedobjectcontext *managedobjectcontext; @property (strong, nonatomic) nsmutablearray *picturelistdata; @property (strong, nonatomic) iboutlet uibutton*scroll; - (void)readdatafortable; -(ibaction)scrolldown:(id)sender; @end @synthesize managedobjectcontext, picturelistdata; @synthesize scroll; // when view reappears, read new data table - (void)viewwillappear:(bool)animated { // repopulate array new table data [self readdatafortable]; } // grab data table - used whenever list appears or reappears after add/edit - (void)readdatafortable { // grab data picturelistdata = [coredatahelper getobjectsforentity:@"pictures" withsortkey:@"title" andsortascending:yes andcontext:managedobjectcontext]; // force table refresh [self.tableview reloaddata]; } #pragma mark - actions // button log out of app (dismiss modal view!) - (ibaction)logoutbuttonpressed:(id)sender { [self dismissmodalviewcontrolleranimated:yes]; } #pragma mark - segue methods - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // reference our detail view picturelistdetail *pld = (picturelistdetail *)[segue destinationviewcontroller]; // pass managed object context destination view controller pld.managedobjectcontext = managedobjectcontext; // if editing picture need pass stuff, check segue title first if ([[segue identifier] isequaltostring:@"editpicture"]) { // row selected view nsinteger selectedindex = [[self.tableview indexpathforselectedrow] row]; // pass picture object table want view pld.currentpicture = [picturelistdata objectatindex:selectedindex]; } } #pragma mark - table view data source // return number of sections in table - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } // return number of rows in section (the amount of items in our array) - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [picturelistdata count]; } // create / reuse table cell , configure display - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } // core data object need use populate table cell pictures *currentcell = [picturelistdata objectatindex:indexpath.row]; // fill in cell contents cell.textlabel.text = [currentcell title]; cell.detailtextlabel.text = [currentcell desc]; // if picture exists use if ([currentcell smallpicture]) { cell.imageview.contentmode = uiviewcontentmodescaleaspectfit; cell.imageview.image = [uiimage imagewithdata:[currentcell smallpicture]]; } return cell; } // swipe delete has been used. remove table item - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { // reference table item in our data array pictures *itemtodelete = [self.picturelistdata objectatindex:indexpath.row]; // delete item in core data [self.managedobjectcontext deleteobject:itemtodelete]; // remove item our array [picturelistdata removeobjectatindex:indexpath.row]; // commit deletion in core data nserror *error; if (![self.managedobjectcontext save:&error]) nslog(@"failed delete picture item error: %@", [error domain]); // delete row data source [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; } } @end
thanks
since dealing dates, it's better create array of dates , find current date using datecomponents.
in snippet dates array of nsdate
instances.
nscalendar *cal = [nscalendar currentcalendar]; nsdate *today = [nsdate date]; [self.dates enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) { nsdate *date = (nsdate *)obj; nsdatecomponents *components = [cal components:nsdaycalendarunit fromdate:date todate:today options:0]; if ([components day]==0) { *stop = true; nsindexpath *indexpath = [nsindexpath indexpathforrow:idx insection:0]; [self.tableview scrolltorowatindexpath:indexpath atscrollposition:uitableviewscrollpositiontop animated:yes]; } }];
edit :
- (void)viewwillappear:(bool)animated { // repopulate array new table data [self readdatafortable]; nscalendar *cal = [nscalendar currentcalendar]; nsdateformatter *dateformatter = [[nsdateformatter alloc]init]; [dateformatter setdateformat:@"dd.mmmm - eeee"]; nsdate *today = [nsdate date]; [picturelistdata enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) { pictures *picture = (pictures *)obj; nsdate *date = [dateformatter datefromstring:picture.title]; nsdatecomponents *components = [cal components:nsdaycalendarunit fromdate:date todate:today options:0]; if ([components day]==0) { *stop = true; nsindexpath *indexpath = [nsindexpath indexpathforrow:idx insection:0]; [self.tableview scrolltorowatindexpath:indexpath atscrollposition:uitableviewscrollpositiontop animated:yes]; } }]; }
Comments
Post a Comment