简体   繁体   English

搜索栏在ios中的设备上不起作用

[英]Search Bar doesn't work on Device in ios

I created iOS(5.0) application in that I have search Bar, which is used to search the content in server side, while passing keyword entered into search bar the list of content parsed from the xml and shown in Table View, 我创建了iOS(5.0)应用程序,其中有搜索栏,该栏用于在服务器端搜索内容,同时将输入的关键字传递给从XML解析并显示在表格视图中的内容列表到搜索栏中,

its working fine in Emulator,.but in device (ipad2 & iphone4s) its not showing table of content were searched. 它在Emulator中工作正常,但是在设备(ipad2和iphone4s)中搜索了未显示的目录。

pls let me know what am making wrong.. 请让我知道出了什么问题。

Thanks in advance,. 提前致谢,。

murali. 穆拉利。

This is mySearchClass.h 这是mySearchClass.h

@interface SearchClass : UIViewController<UITableViewDelegate,UITableViewDataSource,UIPickerViewDelegate,UIPickerViewDataSource,UIActionSheetDelegate,UISearchBarDelegate, UISearchDisplayDelegate>
{
AppDelegate *abc;
MBProgressHUD *HUD;

IBOutlet UIScrollView *scrv;
IBOutlet UITableView *tableV;
UISearchBar *searchBar;
IBOutlet UILabel *lblTitle;
IBOutlet UIActionSheet *actionSheet;
IBOutlet UIPickerView *pickerView;
IBOutlet UIButton *btnActionSheet;
UIToolbar *pickerToolBar;

NSMutableArray *arrCate;
NSString *key;
}

-(void)cleartable;
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
@end

//====================== // ======================

This is my SearchClass.m 这是我的SearchClass.m

@implementation SearchClass

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    self.tabBarItem.image = [UIImage imageNamed:@"search"];

    UIImageView *navImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"i-street"]];
    self.navigationItem.titleView = navImg;
}
return self;
}

- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning];
     }

#pragma mark - View lifecycle

- (void)viewDidLoad
{
   [super viewDidLoad];
   self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage             imageNamed:@"bg1.png"]];
   abc = (AppDelegate*)[[UIApplication sharedApplication]delegate];
   //self.navigationController.navigationBar.tintColor = [UIColor blackColor];

   //[SearchClass initWithNibName:@"" bundle:nil];
   //[SearchClass initWithNibName:@"SearchClass" bundle:nil];

   arrCate = [[NSMutableArray alloc]initWithObjects:@"Arts &    Entertainment",@"Restaurants",@"Bars, Pubs & Clubs",@"Film and Cinema",@"Live Gigs",@"Shops", nil];

   pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43 , 320, 480)];
   pickerView.delegate = self;
   pickerView.dataSource = self;
   [pickerView  setShowsSelectionIndicator:YES];

   pickerToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
   pickerToolBar.barStyle = UIBarStyleBlackOpaque;   
   [pickerToolBar sizeToFit];

   NSMutableArray *barItems = [[NSMutableArray alloc] init];
   UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
   [barItems addObject:flexSpace];

   UIButton *btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
   [btnDone setFrame:CGRectMake(0, 0, 60, 30)];
   [btnDone addTarget:self action:@selector(closeActionSheet)       forControlEvents:UIControlEventTouchUpInside];
   [btnDone setBackgroundImage:[UIImage imageNamed:@"done.png"] forState:UIControlStateNormal];

   UIBarButtonItem *dbtn = [[UIBarButtonItem alloc]initWithCustomView:btnDone];

   [barItems addObject:dbtn];
   [pickerToolBar setItems:barItems animated:YES];
}
-(IBAction)closeActionSheet
{
    lblTitle.text=[arrCate objectAtIndex:[pickerView selectedRowInComponent:0]];
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
     return 1;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row       forComponent:(NSInteger)component {
   return [arrCate objectAtIndex:row];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:   (NSInteger)component
{
   return [arrCate count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:   (NSInteger)component
{

   lblTitle.text = [arrCate objectAtIndex:row];
   [abc.arrSearch removeAllObjects];
   [tableV reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section               {

//    NSLog(@"table view count seasrch :%d",[abc.arrSearch count]);
return [abc.arrSearch count];
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSString *imgUrl;
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    else
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    if(indexPath.row > 0)
    {
        UIImageView *separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line1.png"]];
        [cell.contentView addSubview: separator];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIImageView *imgbest=[[UIImageView alloc]initWithFrame:CGRectMake(213,4,107,64)];

    UIActivityIndicatorView *actSpiner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    actSpiner.center = imgbest.center;
    [actSpiner startAnimating];

    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 20)];
    lbl.textAlignment = UITextAlignmentLeft;
    lbl.font = [UIFont fontWithName:@"Helvetica" size:18.0];
    lbl.textColor = [UIColor whiteColor];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.numberOfLines=0;

    UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(5, 23, 200, 20)];
    lbl1.textAlignment = UITextAlignmentLeft;
    lbl1.font = [UIFont fontWithName:@"American Typewriter" size:13.0];
    lbl1.textColor = [UIColor whiteColor];
    lbl1.backgroundColor = [UIColor clearColor];
    lbl1.numberOfLines=0;

    UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(5,44, 200, 20)];
    lbl2.textAlignment = UITextAlignmentLeft;
    lbl2.font = [UIFont fontWithName:@"Helvetica-Oblique" size:12.0];
    lbl2.textColor = [UIColor whiteColor];
    lbl2.backgroundColor = [UIColor clearColor];
    lbl2.numberOfLines=0;

    if([lblTitle.text isEqualToString:@"Arts & Entertainment"]) 
    {
        ArtsClass *arts=[[ArtsClass alloc]init];                     
        arts = [abc.arrSearch objectAtIndex:indexPath.row];

        imgUrl = arts.artThumpImg;
        lbl.text=arts.artTitle;
        lbl1.text=arts.artDesc;
        lbl2.text=arts.artDate;
    }
    else if([lblTitle.text isEqualToString:@"Restaurants"])
    {
        ResClass *res=[[ResClass alloc]init];                     
        res = [abc.arrSearch objectAtIndex:indexPath.row];

        imgUrl = res.resLogoImg;
        lbl.text=res.resName;
        lbl1.text=res.resType;
        lbl2.text=res.resPopularDish;
    }
    else if([lblTitle.text isEqualToString:@"Bars, Pubs & Clubs"]) 
    {

        BarClass *bar=[[BarClass alloc]init];                     
        bar = [abc.arrSearch objectAtIndex:indexPath.row];

        imgUrl = bar.barImg;
        lbl.text=bar.barTitle;
        lbl1.text=bar.barDesc;
        lbl2.text=bar.barFacilities;  
    }
    else if([lblTitle.text isEqualToString:@"Film and Cinema"])
    {
    FilmClass *film=[[FilmClass alloc]init];                     
    film = [abc.arrSearch   objectAtIndex:indexPath.row];

    imgUrl = film.filmImg;
    lbl.text=film.filmName;
    lbl1.text=film.filmSynopsis;
    lbl2.text=film.filmReleDate;
}
else if([lblTitle.text isEqualToString:@"Live Gigs"])
{
    GigsClass *gigs=[[GigsClass alloc]init];                     
    gigs = [abc.arrSearch objectAtIndex:indexPath.row];
    imgUrl = gigs.eventThumpImg;
    lbl.text=gigs.eventTitle;
    lbl1.text=gigs.eventCate;
    lbl2.text=[NSString stringWithFormat:@"%@ - %@",gigs.eventStartDate,gigs.eventEndDate];
}
else if([lblTitle.text isEqualToString:@"Shops"])
{

    ShopsClass *shops=[[ShopsClass alloc]init];                     
    shops = [abc.arrSearch objectAtIndex:indexPath.row];
    imgUrl = shops.shopThumpImg;
    lbl.text=shops.shopName;
    lbl1.text=shops.shopDesc;
    lbl2.text=shops.shopFacilities;
}
NSURL *url = [NSURL URLWithString:imgUrl] ;                                    
//    NSLog(@"image link:%@",url);
dispatch_queue_t currQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(currQueue,^{
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    dispatch_async(dispatch_get_main_queue(), ^{
        imgbest.image = [UIImage imageWithData:data];
    });
});

[cell addSubview:actSpiner];
[cell addSubview:imgbest];
[cell addSubview:lbl];
[cell addSubview:lbl1];
[cell addSubview:lbl2];

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([lblTitle.text isEqualToString:@"Arts & Entertainment"]) 
    {
    abc.subClassTag=@"ARTS";
    abc.classTag = @"GALLERYCLASS";
    abc.strTitle = @"Arts & Entertainment";
    abc.objArt = [abc.arrArts objectAtIndex:indexPath.row];
    }
    else if([lblTitle.text isEqualToString:@"Restaurants"])
    {
    abc.subClassTag=@"RES";
    abc.classTag = @"GALLERYCLASS";
    abc.strTitle = @"Restaurants";
    abc.objRest = [abc.arrRes objectAtIndex:indexPath.row];
}
else if([lblTitle.text isEqualToString:@"Bars, Pubs & Clubs"])
{
    abc.subClassTag=@"BAR";
    abc.classTag = @"GALLERYCLASS";
    abc.strTitle = @"Bars, Pubs & Clubs";
    abc.objBar = [abc.arrBars objectAtIndex:indexPath.row];
}
else if([lblTitle.text isEqualToString:@"Film and Cinema"])
{
    abc.subClassTag=@"FILMS";
    abc.classTag = @"PLAYERCLASS";
    abc.strTitle = @"Film and Cinema";
    abc.objFilm = [abc.arrFilm objectAtIndex:indexPath.row];
}
else if([lblTitle.text isEqualToString:@"Live Gigs"])
{
    abc.subClassTag=@"GIGS";
    abc.classTag = @"GALLERYCLASS";
    abc.strTitle = @"Live Gigs";
    abc.objGig = [abc.arrGigs objectAtIndex:indexPath.row];
}
else if([lblTitle.text isEqualToString:@"Shops"])
{
    abc.subClassTag=@"SHOPS";
    abc.classTag = @"GALLERYCLASS";
    abc.strTitle = @"Shops";
    abc.objShop = [abc.arrShops objectAtIndex:indexPath.row];
}


if([abc.classTag isEqualToString:@"PLAYERCLASS"])
{
    PlayerClass *pl = [[PlayerClass alloc]init];
    [self.navigationController pushViewController:pl animated:YES];
}
else if([abc.classTag isEqualToString:@"GALLERYCLASS"])
{
    GalaryClass *gc = [[GalaryClass alloc]init];
    [self.navigationController pushViewController:gc animated:YES];
}
}

- (void)viewDidUnload
{
scrv = nil;
tableV = nil;
searchBar = nil;
lblTitle = nil;
[super viewDidUnload];
}

-(IBAction)getCategory:(id)sender 
{
[searchBar resignFirstResponder];
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                          delegate:self
                                 cancelButtonTitle:nil //@"Done"
                            destructiveButtonTitle:nil
                                 otherButtonTitles:nil];

[actionSheet setTag:0];
[actionSheet addSubview:pickerView];
[actionSheet showInView:self.view.superview];
[actionSheet addSubview:pickerToolBar];
[actionSheet showInView:self.view.superview];

[actionSheet setBounds:CGRectMake(0, 0, 320, 430)];
}
-(void)cleartable
{
NSArray * ar=[tableV subviews];
for(int j=0;j<ar.count;j++)
{
    NSArray *arr=[[ar objectAtIndex:j]subviews];
    for(int i=0;i<arr.count;i++)
    {
        if([[arr objectAtIndex:i]isKindOfClass:[UILabel class]])
        {

            [[arr objectAtIndex:i]removeFromSuperview];
        }
    }
}
}


-(void)showProgress
{
//[searchBar resignFirstResponder];

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];

// Set the hud to display with a color
HUD.color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"box4"]];//[UIColor colorWithRed:0.23 green:0.50 blue:0.82 alpha:0.90];
//     HUD.opacity = 
HUD.delegate = self;


[HUD showWhileExecuting:@selector(download) onTarget:self withObject:nil animated:YES];
}

-(void)download
{
if([NetworkManager checkForNetworkStatus])
{
   ParserClass *p =[[ParserClass alloc]init];
   NSLog(@"search bar text  %@ --- %@",key,lblTitle.text);

    if (lblTitle.text==@"Arts & Entertainment")
    {
        [p search:1 string:key];     
    }else if(lblTitle.text==@"Restaurants")
    {
        [p search:5 string:key];
    }else if(lblTitle.text==@"Bars, Pubs & Clubs")
    {
        [p search:4 string:key];
    }else if(lblTitle.text==@"Film and Cinema")
    {
        [p search:2 string:key];
    }else if(lblTitle.text==@"Live Gigs")
    {
        [p search:3 string:key];
    }else if(lblTitle.text==@"Shops")
    {
        [p search:6 string:key];
    }

    [tableV reloadData];

}
else
{
    UIAlertView *alrt = [[UIAlertView alloc]initWithTitle:@"Network Error!" message:@"Please Check your Network connetion.." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alrt show];
}
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText
{
if(![lblTitle.text isEqualToString:@"Select Category"])
{
    if(searchBar.text.length > 0)
    {
        key = searchText;
        [self showProgress];
//            ParserClass *p =[[ParserClass alloc]init];
//            NSLog(@"search bar text  %@ --- %@",searchText,lblTitle.text);

//            if (lblTitle.text==@"Arts & Entertainment")
//            {
//                [p search:1 string:searchText];     
//                }else if(lblTitle.text==@"Restaurants")
//            {
//                [p search:5 string:searchText];
//            }else if(lblTitle.text==@"Bars, Pubs & Clubs")
//            {
//                [p search:4 string:searchText];
//            }else if(lblTitle.text==@"Film and Cinema")
//            {
//                [p search:2 string:searchText];
//            }else if(lblTitle.text==@"Live Gigs")
//            {
//                [p search:3 string:searchText];
//            }else if(lblTitle.text==@"Shops")
//            {
//                [p search:6 string:searchText];
//            }
//            
//            [tableV reloadData];
    }
    else
    {
         UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:nil message:@"Please Give me some Key words..!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
         [alert1 show];
    }
}         
else
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Please Select any Category..!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
}

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1
{
    [searchBar resignFirstResponder];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar1 
{
    [searchBar resignFirstResponder];
}

@end

Thanks for ur Reply,. 感谢您的回复,

Murali. 穆拉利。

wrong string comparison 错误的字符串比较

if (lblTitle.text==@"Arts & Entertainment")

it should be like this - try this it must work & let me know. 应该是这样-尝试此操作必须有效并让我知道。

if (lblTitle.text isEqualToString:@"Arts & Entertainment")

I am also having some issues in my search class, i am using JSON url. 我的搜索类中也遇到了一些问题,我使用的是JSON网址。

  1. Where is the Parsing URL, what are the fields in the xml. 解析URL在哪里,xml中的字段是什么。

  2. Check your parsing url in ur local browser by manual append method using query string ,working or not. 是否使用查询字符串通过手动附加方法在本地浏览器中检查解析网址。

  3. check by trace path, use console log for print the URL. 按跟踪路径检查,使用控制台日志打印URL。

  4. Check if u are using localhost url in ur program somewhere else. 检查您是否在其他地方的程序中使用了localhost url。

  5. Check your delegate for search class. 检查您的代表以进行搜索。

use this link for your reference. 使用此链接供您参考。

https://iphonedevsdk.com/forum/iphone-sdk-development/50468-xml-table-view-searchbar.html https://iphonedevsdk.com/forum/iphone-sdk-development/50468-xml-table-view-searchbar.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM