0

我创建了iOS(5.0)应用程序,其中我有搜索栏,用于搜索服务器端的内容,同时将关键字输入到搜索栏中,从xml解析并显示在表格视图中的内容列表,

它在模拟器中工作正常,但在设备(ipad2 和 iphone4s)中它没有显示的目录被搜索。

请让我知道出了什么问题..

提前致谢,。

壁画

这是 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

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

这是我的 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

谢谢你的回复,。

穆拉利。

4

2 回答 2

1

错误的字符串比较

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

它应该是这样的 - 试试这个它必须工作并让我知道。

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

我的搜索类也有一些问题,我使用的是 JSON url。

于 2013-06-15T12:23:51.540 回答
0
  1. Parsing URL在哪里,xml中的字段是什么。

  2. 通过使用查询字符串的手动附加方法检查您在本地浏览器中的解析 url,工作与否。

  3. 通过跟踪路径检查,使用控制台日志打印 URL。

  4. 检查您是否在其他地方的程序中使用 localhost url。

  5. 检查您的代表的搜索类。

使用此链接供您参考。

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

于 2013-06-15T07:36:56.883 回答