【问题标题】:How to display data on table view on search in iphone如何在 iphone 搜索时在表格视图中显示数据
【发布时间】: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


【解决方案1】:

您需要在 startTest 方法中编辑代码。您正在从 info 数组获取一个对象到 arr 数组。现在,在此之后,您需要使用 *arr 对象的值插入搜索字段名称/邮政编码的值。

例如

if ([arr.name isEqualTo searchField.text] || [arr.zip isEqualTo searchField.text]) 
         {
// stuff to add object to the array which is displayed in the tableview.
} 

并删除坐标内容的代码返回,或者如果需要,您可以评论以备将来使用。

希望对您有所帮助。

【讨论】:

  • 我按照您所说的进行了尝试,但它给出了错误 - 在 if 语句附近使用了未声明的标识符 'arr'。
  • 你应该在这一行下面写代码 GreenValleyInfo *arr = [info objectAtIndex:j];
  • 我添加并在 if 循环中添加了以下行 [resultArray addObject:arr.name];并且我将 brealpoint 保持在循环中,它不仅仅进入循环。
  • 您在哪里捕获搜索值。一些与搜索实例相关的代码。
  • 如果没有进入for循环或者if循环。
猜你喜欢
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多