2012-02-02 24 views
0

下表視圖是我使用的檢查經緯度是否在半徑100米內的代碼,如何將數據發送到根據條件

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [dataArray count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    Person *temp = (Person *)[self.persons objectAtIndex:indexPath.row]; 
    cell.textLabel.text = temp.strName; 

    // Configure the cell... 

    return cell; 
} 



- (void) readDataFromDatabase{ 

    sqlite3 *database; 
    persons = [[NSMutableArray alloc]init]; 

    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK){ 

     const char *sqlStatement = "select * from sdata"; 
     sqlite3_stmt *compiledStatement; 
     if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){ 

      while (sqlite3_step(compiledStatement) == SQLITE_ROW) { 

       pID = sqlite3_column_int(compiledStatement, 0); 
       //    NSLog(@" Id : %d",pID); 
       pName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       NSLog(@" Name : %@",pName); 
       pAdd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
       NSLog(@" Address : %@",pAdd); 
       pPh = sqlite3_column_int(compiledStatement, 3); 
       NSLog(@" PhoneNo : %d",pPh); 
       pCp = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)]; 
       NSLog(@" ContactPerson : %@",pCp); 
       pLat = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)]; 
       NSLog(@" Latitude : %@",pLat); 
       pLong = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)]; 
       NSLog(@" Longitude : %@",pLong); 



       Person *temp = [[Person alloc]initWithID:pID name:pName address:pAdd phone:pPh contactperson:pCp fLat:pLat fLong:pLong]; 
       [persons addObject:temp]; 
       [temp release]; 

      } 

     } 
     sqlite3_finalize(compiledStatement); 
    } 
    sqlite3_close(database); 
} 



- (void)startTest 
{ 
    for(int j=0;j<[persons count];j++){ 

     Person *arr = [persons objectAtIndex:j]; 


     NSString *s = [[NSString alloc]initWithFormat:@"%@ %@ %@",arr.fLat,arr.fLong,arr.strName] ; 
     // NSLog(@" data in array : %@", s); 


#define COORDS_COUNT 1 
#define RADIUS  100.0f 

    CLLocationCoordinate2D coords[COORDS_COUNT] = { 
     CLLocationCoordinate2DMake([arr.fLat floatValue], [arr.fLong floatValue ]), 


    }; 

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake([lat floatValue], [longt floatValue]); 
    CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:center radius:RADIUS identifier:@"Banashankari"]; 



    for (int i = 0; i < COORDS_COUNT; i++) 
    { 
     CLLocationCoordinate2D coord = coords[i]; 

     NSString *coordString = [NSString stringWithFormat:@"%f,%f",coord.latitude, coord.longitude]; 
     [dataArray addObject:coordString]; 

     if ([region containsCoordinate:coord]) 
     { 
      NSLog(@"location %f, %f is within %.0f meters of coord %f, %f", coord.latitude, coord.longitude, RADIUS, center.latitude, center.longitude); 
      [resultArray addObject:coordString]; 
      NSLog(@"data within 100 meters %@",resultArray); 
     } 
     else 
     { 
      NSLog(@"location %f, %f is not within %.0f meters of coord %f, %f", coord.latitude, coord.longitude, RADIUS, center.latitude, center.longitude);  

     } 
    } 

} 
} 

在我的日誌我我只能得到預期結果,即它顯示了哪些經緯度在100米以內,哪些緯度和經度在100米以內,但在我的表格視圖中顯示了所有的緯度和經度,但我需要只顯示僅在100米範圍內的經緯度,在那裏我犯了錯誤。 在此先感謝。

numberOfRowsInSection你正在服用dataArray中的 計數的方法和方法
+0

顯示你的tableview的代碼。 – Sarah 2012-02-02 06:55:41

+0

@Sarah請檢查我已更新的代碼。 – shasha 2012-02-02 07:02:17

回答

0

cellForRowAtIndexPath您加載

Person *temp = (Person *)[self.persons objectAtIndex:indexPath.row]; 
cell.textLabel.text = temp.strName; 

應該

cell.textLabel.text = [dataarray objectAtIndex:indexPath.row]; 

試試這個。如果不解決,告訴我。

+0

它顯示的所有值仍然在表視圖 – shasha 2012-02-02 07:27:49

+0

是...使用resultArray而不是數組數組並保留resultArray [resultArray addObject:coordString]後; – Sarah 2012-02-02 07:29:32

+0

其拋出以下異常終止應用程序由於未捕獲異常'NSRangeException',原因:'*** - [NSMutableArray objectAtIndex:]:索引3超越界限[0..2]' – shasha 2012-02-02 07:33:04