xcode - Add Distance from current location to NSMutableArray -


i'm trying load distance current location array, seem missing something. tableview shows "?", should show if currentloc not working, nslog showing correct location. please help

update: must have deleted reload tableview, fixed "?", 0.00 km. still missing something. currentloc correct inside locationmanager, lose when try calculate distance in array.

#import "authorviewcontroller.h" #import "author.h" #import <sqlite3.h>   @implementation authorviewcontroller @synthesize theauthors; @synthesize currentloc;  - (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation { currentloc = newlocation;  if (newlocation.horizontalaccuracy <= 100.0f) {     [locmgr stopupdatinglocation]; } nslog(@"lat: %f, long: %f", currentloc.coordinate.latitude, currentloc.coordinate.longitude);  [self.tableview reloaddata]; }  - (void)locationmanager:(cllocationmanager *)manager didfailwitherror:(nserror *)error { nsstring *msg = [[nsstring alloc]initwithstring:@"error obtaining location"]; uialertview *alert = [[uialertview alloc]initwithtitle:@"error" message:msg delegate:nil cancelbuttontitle:@"done" otherbuttontitles:nil]; [alert show]; }   #pragma mark - view lifecycle  - (void)viewdidload { [self barlist]; [super viewdidload];  nslog(@"lat2: %f, long2: %f", currentloc.coordinate.latitude, currentloc.coordinate.longitude); }  - (void)viewwillappear:(bool)animated { [super viewwillappear:animated];  locmgr = [[cllocationmanager alloc] init]; locmgr.delegate = self; [locmgr startupdatinglocation]; }   #pragma mark - table view data source  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {  // return number of rows in section. return [self.theauthors count]; }  - (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]; } int rowcount = indexpath.row;  author *author = [self.theauthors objectatindex:rowcount];    cell.textlabel.text = author.barname;  if (currentloc == nil) {     cell.detailtextlabel.text = [nsstring stringwithformat:@"?"]; }else {     cell.detailtextlabel.text = [nsstring stringwithformat:@"%.02f km", author.cacheddist]; }   return cell; }   -(nsmutablearray *) barlist{ theauthors = [[nsmutablearray alloc] init]; @try {     nsfilemanager *filemgr = [nsfilemanager defaultmanager];     nsstring *dbpath = [[[nsbundle mainbundle] resourcepath ]stringbyappendingpathcomponent:@"tulsabars.sqlite"];     bool success = [filemgr fileexistsatpath:dbpath];     if(!success)     {         nslog(@"cannot locate database file '%@'.", dbpath);     }     if(!(sqlite3_open([dbpath utf8string], &db) == sqlite_ok))     {         nslog(@"an error has occured: %@", sqlite3_errmsg(db));      }       const char *sql = "select *  tulsabars";     sqlite3_stmt *sqlstatement;     if(sqlite3_prepare(db, sql, -1, &sqlstatement, null) != sqlite_ok)     {         nslog(@"problem prepare statement:  %@", sqlite3_errmsg(db));     }else{          while (sqlite3_step(sqlstatement)==sqlite_row) {             author * author = [[author alloc] init];             author.barname = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement,1)];             author.baraddress = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement,2)];             author.barstate = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement, 5)];             author.barlat = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement, 8)];             author.barlong = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement, 9)];             author.cacheddist = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement, 10)];              cllocation *barlocation = [[cllocation alloc] initwithlatitude:[author.barlat doublevalue] longitude:[author.barlong doublevalue]];             cllocationdistance distancekm = [currentloc distancefromlocation: barlocation]/1000;             nsstring *distancestring = [[nsstring alloc] initwithformat: @"%f", distancekm];             author.cacheddist = distancestring;              [theauthors addobject:author];         }     }     sqlite3_finalize(sqlstatement); } @catch (nsexception *exception) {     nslog(@"problem prepare statement:  %@", sqlite3_errmsg(db)); } @finally {     sqlite3_close(db);      return theauthors; } }  @end 

make sure call [tableview reloaddata] when need redraw data in table view.

you might try extended techniques outlined in answer: what's best way refresh uitableview within uinavigationcontroller hierarchy


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -