2

検索バーを使用してテーブルビューを設定しています。すべてがうまく機能します。検索バーにテキストを入力すると、検索テキストに基づいてテーブルビューの結果が更新されます。私が抱えている問題は、詳細ビューに移動するときに、テーブルセルのIDがそれを渡すことを決定することです。明確にするために:テーブルビューのセルは通常、SQLite3データベースから生成され、名前順に並べられています。したがって、4つのセル(Cabernet Cortis、Cabernet Franc、Cabernet Sauvignon、Ruby Cabernet)がある場合、それらのIDは通常(78、79、80、378)です。ただし、「cabernet」を検索してこれらの4つの項目が表示されると、indexPath.rowは(78、79、80、378)ではなく(1、2、3、4)になるため、詳細ビュー(私のDBに入力されたアルファ順の最初の4つのブドウ)。

これが物事を片付けるのを助けるためのいくつかのコードです。[actualIDNumber objectAtIndex:indexPath.row]検索結果として表示される各ブドウのIDを取得し、それらの値のMSMutableArrayを取得して、の代わりに使用できると便利だと思いますindexPath.row

//ViewGrapesViewController.h
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
- (void) searchGrapesTableView;
@property (nonatomic, retain) NSMutableArray *listOfGrapes;
@property (nonatomic, retain) NSMutableArray *searchResult;
@property (nonatomic, retain) NSMutableArray *searchResultID;
@property (nonatomic) Boolean isSearchOn;

//ViewGrapesViewController.m
//synthesize variables, etc
- (void)viewDidLoad
    {
        //obtain grapes from the DB, etc.
        //add searchNames to an NSMutableArray
        listOfGrapes = [[NSMutableArray alloc] init];
        for (int i=1; i<=[self.grapes count]; i++) {
            Grapes *theGrape = [self.grapes objectAtIndex:(i-1)];
            NSString *grapeSearchName = theGrape.searchName;
            [listOfGrapes addObject:grapeSearchName];
        }
        searchResult = [[NSMutableArray alloc] init];
        searchResultID = [[NSMutableArray alloc] init];
        isSearchOn = NO;

    //search bar stuff
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

    [super viewDidLoad];
}

- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    isSearchOn = YES;
}

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ([searchText length] > 0) {
        isSearchOn = YES;
        [self searchGrapesTableView];
    }
    else {
        isSearchOn = NO;
    }
    [self.tableView reloadData];
}

- (void) searchGrapesTableView {
    [searchResult removeAllObjects];
    [searchResultID removeAllObjects];
    for (NSString *str in listOfGrapes)
    {
        NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0) {
            [searchResult addObject:str];
            //would love to add ID to searchResultID array here, don't know how to do that
        }
    }
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchGrapesTableView];
}

- (void) searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    isSearchOn = NO;
    [self.searchBar resignFirstResponder];
    [self.tableView reloadData];
}

//table view code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //first check if the user is searching
    if (isSearchOn) {
        return 1;
    }
    //if not, then search by alpha order
    else {
        return 24;
    }
}

//create header titles & number of rows in section (it's a lot of code and works fine)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"grapeCell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //is the user searching?
    if (isSearchOn) {
        NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
    }

    //otherwise...
    else {
        NSInteger theNumber;
        NSInteger numberToAdd = [[howManyRowsInSection objectAtIndex:indexPath.section] integerValue];
        theNumberToAdd = numberToAdd;
        theNumber = numberToAdd + indexPath.row;

        //cell title = grape name
        Grapes *grape = [self.grapes objectAtIndex:theNumber];
        NSString *theGrapeName = grape.grapeName;
        cell.textLabel.text = theGrapeName;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //is the user searching?
    if (isSearchOn) {
        theNumberToAdd=0; //which is wrong but here for the time being
        //i have to have the ID here in order to do some logic to figure out what row to return
    }

    //otherwise...
    else {
        theNumberToAdd = [[howManyRowsInSection objectAtIndex:indexPath.section] integerValue]; // i performed this logic elsewhere
    }


    ViewGrapeViewController *detailViewController = (ViewGrapeViewController *)self.presentedViewController;
    detailViewController.detailItem = [self.grapes objectAtIndex: (indexPath.row+theNumberToAdd)];
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"toGrapeDetail"]) {       
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        //is the user searching
        if (isSearchOn) {
            theNumberToAdd=0;  // which is wrong
        }

        //otherwise...
        else {
            theNumberToAdd = [[howManyRowsInSection objectAtIndex:indexPath.section] integerValue];
        }

        NSDate *object = [_objects objectAtIndex:(indexPath.row+theNumberToAdd)];
        [[segue destinationViewController] setDetailItem:object];
    }
}
4

2 に答える 2

1

did select itemメソッドで、選択されたセルからテキストを取得し、一致するアイテムが見つかるまで配列を反復処理してから、そのセルの詳細ビューを開きます。これは、すべての一意のアイテム名がある場合にのみ機能します。

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
    for (int j=0; j<whateverArray.count; j++)
    {
        NSString *temp = [whateverArray objectAtIndex:j];
        if ([str isEqualToString:temp])
        {
            //do whatever you would do if the user actually clicked the row at position j
        }
    }
}
于 2012-07-23T17:26:13.350 に答える
0

アレイ数に相関する各セルの連続タグ番号を作成することもできます。次に、タグ番号を取得して、インデックスのオブジェクトに使用します。

これは、各文字列を検索するよりもわずかに効率的であり、重複する文字列名に対応できます。

コードは次のようになります:

 - (UITableViewCell *) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionCellIdentifier];
 cell.tag = i //where i increments by one each entry into the function.
 }
于 2012-07-23T21:22:25.517 に答える