如何找出我在搜索过程中单击的 UITableViewCell? [英] How can I figure out what UITableViewCell I'm clicking on during a search?

查看:22
本文介绍了如何找出我在搜索过程中单击的 UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a table view set up with a search bar. Everything works great: when I type text into the search bar, the results update in the table view based on my search text. The problem I'm having is in determining what the ID of the table cell is to pass that along when I'm going to my detail view. To clarify: the cells in the table view are usually generated from a SQLite3 database and ordered by their name. So if I have 4 cells: (Cabernet Cortis, Cabernet Franc, Cabernet Sauvignon and Ruby Cabernet), their IDs are normally (78, 79, 80, 378). However when I search "cabernet", and these four items come up, the indexPath.row is (1, 2, 3, 4) instead of (78, 79, 80, 378) so it's taking me to the wrong grape in the detail view (which are the first four grapes in alpha order as entered into my DB).

这里有一些代码可以帮助解决问题.我认为如果我可以获得作为搜索结果出现的每个葡萄的 ID 并且还有这些值的 MSMutableArray,然后使用 [actualIDNumber objectAtIndex:indexPath.row] 代替只是indexPath.row.

Here is some code to help clear things up. I think what would help is if I can obtain the ID of each grape that comes up as a search result and also have an MSMutableArray of those values, and then use [actualIDNumber objectAtIndex:indexPath.row] instead of just 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];
    }
}

推荐答案

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

In your did select item method, get the text from the cell that was selected and iterate through your array until you find an item that matches, then open that cell's detail view. This only will work if you have all unique item names.

- (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
        }
    }
}

这篇关于如何找出我在搜索过程中单击的 UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆