【发布时间】:2012-03-19 16:25:48
【问题描述】:
我有一个项目,其中有一个表格视图、文本字段和按钮,并且我在项目中有一个 sqlite 数据库。在我的数据库中,我有名称、城市、州、邮编、纬度和经度等字段。目前我正在显示纬度和经度的名称,当用户在文本字段中给出 100 时,即用户可以给出任何米 100 或 200,这取决于米它根据当前纬度显示相应纬度和经度的名称和经度。 为此,我使用以下代码,
- (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 [resultArray 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];
}
cell.textLabel.text = [resultArray objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
- (void) readDataFromDatabase{
sqlite3 *database;
info = [[NSMutableArray alloc]init];
if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK){
const char *sqlStatement = "select * from tourism";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){
NSLog(@"SQLITE_OK");
while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
pID = sqlite3_column_int(compiledStatement, 0);
pName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSLog(@"%@",pName);
pAdd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSLog(@"%@",pAdd);
pCity = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSLog(@"%@",pCity);
pState = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSLog(@"%@",pState);
pZip = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
NSLog(@"%@",pZip);
pWebsite = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
NSLog(@"%@",pWebsite);
pCategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];
NSLog(@"%@",pCategory);
pLat = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];
NSLog(@"%@",pLat);
pLong = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)];
NSLog(@"%@",pLong);
pGeolocation = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)];
NSLog(@"%@",pGeolocation);
GreenValleyInfo *temp = [[GreenValleyInfo alloc]initWithUniqueId:(int)pID name:(NSString *)pName address:(NSString *)pAdd city:(NSString *)pCity state:(NSString *)pState zip:(NSString *)pZip website:(NSString *)pWebsite category:(NSString*)pCategory latitude:(NSString *)pLat longitude:(NSString *)pLong geolocation:(NSString *)pGeolocation];
[info addObject:temp];
[temp release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
开始测试
- (void)startTest
{
[resultArray removeAllObjects];
for(int j=0;j<[info count];j++){
GreenValleyInfo *arr = [info objectAtIndex:j];
RADIUS = [Proxy.text intValue];
CLLocationCoordinate2D coords[COORDS_COUNT] = {
CLLocationCoordinate2DMake([arr.latitude floatValue], [arr.longitude floatValue]),
};
CLLocationCoordinate2D center = CLLocationCoordinate2DMake([lat floatValue], [longt floatValue]);
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:center radius:RADIUS identifier:@"Locations"];
CLLocationCoordinate2D coord = coords[0];
if ([region containsCoordinate:coord])
{
NSLog(@"location %f, %f,%@ is within %i meters of coord %f, %f", coord.latitude, coord.longitude,arr.name, RADIUS, center.latitude, center.longitude);
[resultArray addObject:arr.name];
}
else
{
NSLog(@"location %f, %f,%@ is not within %i meters of coord %f, %f", coord.latitude, coord.longitude,arr.name, RADIUS, center.latitude, center.longitude);
}
}
}
但现在我需要更改搜索方法,即现在我需要在文本字段中提供城市或 zip 而不是提供米数,并且它必须在表格视图中显示相应的名称。
提前致谢。
【问题讨论】:
标签: iphone uitableview search sqlite