我有一个项目,其中有一个表视图和textfield和按钮,我在项目中有一个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);
}
}
}
但是现在我需要更改搜索方法,即现在我需要在文本字段中给出城市或邮编而不是给出米,而是必须在表格视图中显示相应的名称。
提前致谢。
答案 0 :(得分:0)
您需要在startTest方法中编辑代码。您将从info数组获取一个对象到arr数组。此后,您需要检查插入的搜索字段名称/ zipcode的值是否为* arr object。
例如
if ([arr.name isEqualTo searchField.text] || [arr.zip isEqualTo searchField.text])
{
// stuff to add object to the array which is displayed in the tableview.
}
并删除协调内容的代码返回,或者如果需要,您可以评论以备将来使用。
希望这对你有所帮助。