2

我有一个带有搜索栏的表格视图。一切都很好:当我在搜索栏中输入文本时,表格视图中的结果会根据我的搜索文本进行更新。我遇到的问题是在我转到详细视图时确定表格单元格的 ID 是什么来传递它。澄清一下:表格视图中的单元格通常是从 SQLite3 数据库生成的,并按其名称排序。因此,如果我有 4 个细胞:(Cabernet Cortis、Cabernet Franc、Cabernet Sauvignon 和 Ruby Cabernet),它们的 ID 通常是(78、79、80、378)。但是,当我搜索“赤霞珠”时,这四个项目出现了,indexPath.row 是 (1, 2, 3, 4) 而不是 (78, 79, 80, 378) 所以它把我带到了错误的葡萄详细视图(这是输入到我的数据库中的 alpha 顺序的前四个葡萄)。

这是一些有助于清除问题的代码。我认为如果我可以获得作为搜索结果出现的每个葡萄的 ID 并且还具有这些值的 MSMutableArray,然后使用[actualIDNumber objectAtIndex:indexPath.row]而不是仅使用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

在您的选择项目方法中,从所选单元格中获取文本并遍历您的数组,直到找到匹配的项目,然后打开该单元格的详细视图。仅当您拥有所有唯一的项目名称时,这才有效。

- (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 回答