如何找出我在搜索过程中点击了哪些UITableViewCell?
我有一个表格视图与搜索栏设置。一切都很好:当我在搜索栏中输入文本时,结果将根据我的搜索文本在表格视图中更新。我遇到的问题是在确定表格单元的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),所以它将我带到错误的葡萄中详细视图(这是按照输入到我的数据库中的顺序排列的前四个葡萄)。如何找出我在搜索过程中点击了哪些UITableViewCell?
下面是一些代码,以帮助清理事情。我认为,如果我可以获得每个出现搜索结果的葡萄的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];
}
}
在你做了选择项目的方法,从那个被选中,并通过您的数组进行迭代,直到找到匹配的项目单元格中的文本,然后打开电池的详细视图。如果您拥有所有独特的商品名称,这只会起作用。
- (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
}
}
}
是的!一个灯泡刚刚在我脑海中熄灭!谢谢! – lemontwist 2012-07-23 17:29:48
更新了回答,代码 – Dustin 2012-07-23 17:33:41
Thx,这太好了。 :-D – lemontwist 2012-07-23 17:35:01
您还可以为与阵列计数相关的每个单元格创建一个顺序标签号。然后,您抓住标签号码并将其用于索引处的对象。
这比搜索每个字符串的效率稍高一些,它可以容纳重复的字符串名称。
代码将是:
- (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.
}
我看不出这将如何帮助确定我在searchResult数组中查找的对象。 – lemontwist 2012-07-26 16:53:31
考虑与数组直接相关的每个单元格的标签。如果你愿意的话,1比1。 示例[数组objectforindex:1]等于cell.tag = 1.当您在函数didSelectRowAtIndexPath中时,您将获得单元格。标记值应该直接与数组中的索引相关联,并使用正确的值。凉? – 2012-07-27 19:11:20
例如我考虑使用'NSUInteger指数= [self.grapes indexOfObject:STR];',不同之处在于它只会返回最低索引,我知道至少对于搜索词Cabernet,将会有4个我需要返回的ID。 – lemontwist 2012-07-23 17:26:41