ios - 搜索栏和 Mysql

标签 ios mysql

iOS 新手...我正在尝试使用搜索栏对下载的 NSMutableArray 表进行搜索,或者确实需要从 mysql 表中进行搜索...下面的代码让我发疯...任何帮助都是赞赏

#import "VendorViewController.h"
#import "VendLocation.h"
#import "VendorDetailController.h"

@interface VendorViewController () 
{
    VendorModel *_VendorModel; NSMutableArray *_feedItems; VendLocation *_selectedLocation; UIRefreshControl *refreshControl;
}
@property (nonatomic, weak) IBOutlet UISearchBar *searchBar;
@end

@implementation VendorViewController
//@synthesize item;

- (void)viewDidLoad
{
   [super viewDidLoad];
    self.title =  @"Vendors";
    self.searchBar.hidden = YES;
    self.searchBar.delegate = self;
    self.searchBar.barTintColor = [UIColor clearColor];
    //self.searchBar.barTintColor = [UIColor orangeColor];

    _feedItems = [[NSMutableArray alloc] init];
    _VendorModel = [[VendorModel alloc] init];
    _VendorModel.delegate = self;
    [_VendorModel downloadItems];

#pragma mark  Bar Button
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchButton:)];
    NSArray *actionButtonItems = @[searchItem];
    self.navigationItem.rightBarButtonItems = actionButtonItems;

#pragma mark  Table Refresh
    UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.tableView insertSubview:refreshView atIndex:0]; //the tableView is a IBOutlet
    refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.backgroundColor = [UIColor grayColor];
    refreshControl.tintColor = [UIColor whiteColor];
    [refreshControl addTarget:self action:@selector(reloadDatas) forControlEvents:UIControlEventValueChanged];
    NSMutableAttributedString *refreshString = [[NSMutableAttributedString alloc] initWithString:@"Refreshing"];
    //add date to refresh
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMM d, h:mm a"];
    NSString *lastUpdated = [NSString stringWithFormat:@"Last updated on %@", [formatter stringFromDate:[NSDate date]]];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];

    [refreshString addAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} range:NSMakeRange(0, refreshString.length)];
    [refreshView addSubview:refreshControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

**#pragma mark - Search
- (void)searchButton:(id)sender{
   [self.searchBar becomeFirstResponder];
    self.searchBar.hidden = NO;
   //[self.listTableView resignFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    self.searchBar.hidden = YES;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if(searchText.length == 0)
    {
        isFilltered = NO;
    } else {
        isFilltered = YES;
        filteredString = [[NSMutableArray alloc]init];
         for(NSString *vendorName in _feedItems)
        {
            NSRange vendorNameRange = [vendorName rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(vendorNameRange.location != NSNotFound) {
                [filteredString addObject:vendorName];
            }
        }
    }
    [self.listTableView reloadData];
}**

#pragma mark - Table
-(void)reloadDatas {
    [refreshControl endRefreshing];
}

-(void)itemsDownloaded:(NSMutableArray *)items
{   // This delegate method will get called when the items are finished downloading
    _feedItems = items;
    [self.listTableView reloadData];
}

#pragma mark  Table Delete Button
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleDelete;
}

- (void) setEditing:(BOOL)editing
           animated:(BOOL)animated{

    [super setEditing:editing
             animated:animated];

    [self.listTableView setEditing:editing
                        animated:animated];

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle == UITableViewCellEditingStyleDelete) {
    [_feedItems removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath]
                         withRowAnimation:UITableViewRowAnimationLeft];
    [self.tableView reloadData];
      /*
        NSError *error = nil;
        if (![tableView save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        } */
    }
}

#pragma mark  TableView Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (isFilltered) {
        return [filteredString  count];
       }
        return _feedItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VendLocation *item = _feedItems[indexPath.row];
    static NSString *CellIdentifier = @"BasicCell";
    UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    myCell.layer.cornerRadius = 5;
    myCell.layer.masksToBounds = YES;

    if (myCell == nil) {
        myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }

    if (!isFilltered) {
        myCell.textLabel.text = item.vendorName;
        myCell.detailTextLabel.text = item.vendorNo;

        //Retreive an image
        UIImage *myImage = [UIImage imageNamed:@"DemoCellImage"];
        [myCell.imageView setImage:myImage];
    } else {
      myCell.textLabel.text = [filteredString objectAtIndex:indexPath.row];
      myCell.detailTextLabel.text = nil;
    }
    return myCell;
}

#pragma mark  Tableheader
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 55.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *newString = [NSString stringWithFormat:@"VENDOR \n%lu", (unsigned long) _feedItems.count];
    NSString *newString1 = [NSString stringWithFormat:@"NASDAQ \n4,727.35"];
    NSString *newString2 = [NSString stringWithFormat:@"DOW \n17,776.80"];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 0)];
    tableView.tableHeaderView = view; //makes header move with tablecell
    [view setBackgroundColor:[UIColor clearColor]];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 3, tableView.frame.size.width, 45)];
    [label setFont:[UIFont systemFontOfSize:12]];
    [label setTextColor:[UIColor whiteColor]];
    label.numberOfLines = 0;
    NSString *string = newString;
    [label setText:string];
    [view addSubview:label];

    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(12, 45, 60, 1.5)];
    separatorLineView.backgroundColor = [UIColor redColor];
    [view addSubview:separatorLineView];

    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(85, 3, tableView.frame.size.width, 45)];
    label1.numberOfLines = 0;
    [label1 setFont:[UIFont systemFontOfSize:12]];
    [label1 setTextColor:[UIColor whiteColor]];
    NSString *string1 = newString1;
    [label1 setText:string1];
    [view addSubview:label1];

    UIView* separatorLineView1 = [[UIView alloc] initWithFrame:CGRectMake(85, 45, 60, 1.5)];
    separatorLineView1.backgroundColor = [UIColor redColor];
    [view addSubview:separatorLineView1];

    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(158, 3, tableView.frame.size.width, 45)];
    label2.numberOfLines = 0;
    [label2 setFont:[UIFont systemFontOfSize:12]];
    [label2 setTextColor:[UIColor whiteColor]];
    NSString *string2 = newString2;
    [label2 setText:string2];
    [view addSubview:label2];

    UIView* separatorLineView2 = [[UIView alloc] initWithFrame:CGRectMake(158, 45, 60, 1.5)];
    separatorLineView2.backgroundColor = [UIColor redColor];
    [view addSubview:separatorLineView2];

    return view;
}

这是我的标题

#import <UIKit/UIKit.h>
#import "VendorModel.h"

@interface VendorViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, VendorModelProtocol>
{
   // NSMutableArray *_feedItems;
    NSMutableArray *filteredString;
    BOOL isFilltered;
}

@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

将代码更改为textDidChange

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0)
{
    isFilltered = NO;
} else {
    isFilltered = YES;
    filteredString = [[NSMutableArray alloc]init];

     for(VendLocation* vendor in _feedItems)
    {
        NSRange stringRange = [vendor.vendorName rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if(stringRange.location != NSNotFound) {
            [filteredString addObject:vendor];
        }
    }
}
[self.tableView reloadData];
}

我也改变了过滤器这个代码仍然没有运气击中一个键并崩溃

if(searchText.length == 0)
{
    isFilltered = NO;

    [filteredString removeAllObjects];
    [filteredString addObjectsFromArray:_feedItems];

} else {
    isFilltered = YES;
   // filteredString = [[NSMutableArray alloc]init];
    [filteredString removeAllObjects];
    for(NSString* string in _feedItems)
    {
        NSRange stringRange = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if(stringRange.location != NSNotFound) {
            [filteredString addObject:string];
        }
    }
}
[self.tableView reloadData];

最佳答案

如果您已经下载了一个包含数据的 NSMutableArray,那么您的搜索可以仅包括搜索数组并过滤掉不匹配的条目。您不必返回 MySQL。

关于ios - 搜索栏和 Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27319065/

相关文章:

ios - 标签的 UIButton 文本不可见

ios - RxSwift 绑定(bind)到变量的格式化值

mysql - SQL 将结果限制为前 50%

mysql - 设置 Rails 以查找 LIBMYSQL.dll 的默认位置

ios - 在 Xcode 9/iOS 11 中将应用程序图标自定义为构建阶段

iphone - 以编程方式实现 UIScrollView

ios - 添加自定义手势识别器以在 Xcode Interface Builder 中查看?

mysql - 基于 SQL (BigQuery) 中的多个列返回许多小数据样本

MYSQL如何联合多个 "Unknown"表

MySQL 到 MongoDB 的转换