SearchBar和Mysql

时间:2014-12-05 15:18:07

标签: 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];

1 个答案:

答案 0 :(得分:0)

如果您已经下载了包含数据的NSMutableArray,那么您的搜索可能只包括搜索数组并过滤掉不匹配的条目。你不必回到MySQL。