如何根据条件将数据发送到表视图

时间:2012-02-02 05:21:52

标签: objective-c uitableview latitude-longitude

以下是我用来检查纬度和经度是否在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米范围内的纬度和经度,我在做错误。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

在方法numberOfRowsInSection中您正在加载 count of dataArray ,而在方法cellForRowAtIndexPath中正在加载

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

应该是

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

试试这个。如果没有解决,请告诉我。