Searchbar UITableView包含来自数据库的2个不同数据表

时间:2013-08-15 06:27:59

标签: objective-c search

现在我正在创建一个搜索栏。我使用JSON从我的数据库中检索数据。 目前我有这些

    cell.textLabel.text = place.parkName;
cell.detailTextLabel.text = place.parkDesc;

如何使cell.textlabel.text继续使用来自其他数据数组的数据?

编辑:抱歉我的问题有点令人困惑,我的意思是我目前有搜索栏工作,但它只搜索数据库中的1个表。我想要实现的是从数据库中的2个表中搜索。

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
    isFiltered = FALSE;
}
else
{
    isFiltered = TRUE;
    filteredTableData = [[NSMutableArray alloc] init];

    for (Places* place in placeArray)
    {
        NSRange nameRange = [place.parkName rangeOfString:text options:NSCaseInsensitiveSearch];
        NSRange descriptionRange = [place.parkDesc rangeOfString:text options:NSCaseInsensitiveSearch];
        if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
        {
            [filteredTableData addObject:place];
        }
    }
}

[self.tableView reloadData];
}

#pragma mark - Table View Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{  
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return isFiltered ? searchedData.count : json.count;
//return json.count;

int rowCount;
if(self.isFiltered)
    rowCount = filteredTableData.count;
else
    rowCount = placeArray.count;

return rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell==nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:CellIdentifier];
}

//cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];

//retrieve the current city object for use with this indexpath.row


cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

Places* place;
if(isFiltered)
    place = [filteredTableData objectAtIndex:indexPath.row];
else
    place = [placeArray objectAtIndex:indexPath.row];

cell.textLabel.text = place.parkName;
cell.detailTextLabel.text = place.parkDesc;

return cell;
}

1 个答案:

答案 0 :(得分:0)

我认为你是以错误的方式思考这个问题。而不是得到一个然后另一个,为什么不只是得到两个并合并数组?

Merge Arrays Documentation

因此,简而言之,只需将“placeArray”数组更改为合并的两个数组的“数据”数组。

如果两者之间的搜索不同,则为int i = 0制作一个for循环;我< [placeArray count]; i ++然后是下一个循环i = [placeArray count];我< ([placeArray count] + [shopArray count] - 1);我+ +

如果您有任何疑问,请询问。我很高兴编辑这个。