如果找不到该项,如何创建“添加(搜索项目)”

时间:2013-12-07 21:31:53

标签: ios iphone objective-c uisearchbar

嗨,我一直在寻找一种可以做到这样的事情...... 在UISearchBar上,键入“Product”之类的单词,如果找不到 在第一个单元格中,它应该显示为“Add(Product)”,如果我点击它, 将创建新项目(产品)......

这就是我想得到的......

http://imgur.com/1f9OE3V

我一直试图通过使用SearchBar和搜索显示控制器来找到添加按钮的信息,以便这样做..但我没有找到任何东西..

请帮帮我! :)

2 个答案:

答案 0 :(得分:1)

您不应该添加按钮,应该检查已过滤数组的数量(用于填充搜索表),如果为零,则在该数组中添加一个字符串“添加+搜索文本”。然后在didSelectRowAtIndexPath中,您需要区分是选择了“普通”搜索结果表格单元格,还是选择了自己添加self.addTextString的单元格。像这样的东西(addTextString是我添加的属性):

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

    DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
    if(tableView == self.tableView) {
        //do something from unfiltered table
    }
    if(tableView == self.searchDisplayController.searchResultsTableView) {
        if ([self.filteredData[indexPath.row] isEqualToString:self.addTextString]) {
            AddProductViewController *add = [self.storyboard instantiateViewControllerWithIdentifier:@"AddProduct"];
            [self.navigationController pushViewController:add animated:YES];
        }else{
            detail.detailText = self.filteredData[indexPath.row];
            [self.navigationController pushViewController:detail animated:YES];
        }
    }
}


#pragma mark UISearchDisplayController Delegate Methods

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {

    [self.filteredData removeAllObjects];
    for (NSString *s in self.theData) {
        if ([s compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])] == NSOrderedSame) {
            [self.filteredData addObject:s];
        }
    }
    if (self.filteredData.count == 0) {
        [self.filteredData addObject:[@"Add " stringByAppendingString:searchText]];
        self.addTextString = [@"Add " stringByAppendingString:searchText];
    }
}

答案 1 :(得分:0)

您可以检查搜索的项目数组计数是否为0.如果确实为0,则可以:

  1. -numberOfRowsInSection:中返回1并正确处理-didSelectRowAtIndexPath:

  2. 插入“添加[ SearchTerm ]”并妥善处理