0

我正在尝试使用搜索栏过滤表格视图。我已经写了所有必要的方法。

我的代码如下:

它有什么问题?请帮助我,我找不到解决方案。

应该有一些我想念的东西

@implementation ApothekeViewController
@synthesize  tableView,searchBar,mapView,eczaneler,eczanelerTemp,locationManager,searchDispController,filteredListContent,isFiltered,tableViewController;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
    tableViewController = [[UITableViewController alloc] init];
    tableViewController.tableView.delegate = self;
    tableViewController.tableView.dataSource = self;
    [tableViewController.tableView reloadData];
    tableViewController.tableView.frame = CGRectMake(0,66 ,320.0, 768.0);

    // tableView = [[UITableView alloc]init];
    //tableView.frame = CGRectMake(0,66 ,320.0, 768.0);
    //tableView.delegate = self;
    //tableView.dataSource = self;

    [self.view addSubview:tableViewController.tableView];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.placeholder = @"Arama" ;
    searchBar.delegate = self;

    UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
                                            initWithSearchBar:searchBar
                                            contentsController:tableViewController];
    self.searchDispController = searchCon;

    searchDispController.delegate = self;
    searchDispController.searchResultsDataSource = self;
    searchDispController.searchResultsDelegate = self;

    //searchBar.showsCancelButton = YES;

    tableViewController.tableView.tableHeaderView = searchBar;

    NSDate *now = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init ] ;
    [formatter setDateFormat:@"dd MMMM yyyy, EEEE"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]];

    NSString *strSelectedDate= [formatter stringFromDate:now];



    UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 55)];

    UILabel *date = [[UILabel alloc] initWithFrame:CGRectMake(760,25,400,30)];
    date.text = strSelectedDate;
    date.textColor = [UIColor whiteColor];
    [date setFont:[UIFont fontWithName:@"Arial" size:25]];
    [naviBarObj addSubview:date];
    [date setBackgroundColor:[UIColor clearColor]];
    naviBarObj.topItem.titleView = date;

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(60,5,400,50)];
    title.text = @"En Yakın Eczane";
    title.textColor = [UIColor whiteColor];
    [title setFont:[UIFont fontWithName:@"Arial" size:40]];
    [naviBarObj addSubview:title];
    [title setBackgroundColor:[UIColor clearColor]];
    naviBarObj.topItem.titleView = title;


    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
                                  initWithTitle:@"Cancel"
                                  style:UIBarButtonItemStyleBordered
                                  target:self
                                  action:@selector(cancel_Clicked:)];

    naviBarObj.topItem.leftBarButtonItem = btnCancel;

    [self.view addSubview:naviBarObj];


}
return self;
}


- (void)viewDidLoad
{
[super viewDidLoad];



mapView = [[MKMapView alloc] initWithFrame:CGRectMake(320, 66, 704, 705)];

mapView.delegate = self;

[self.view addSubview:mapView];

mapView.showsUserLocation = YES;

eczanelerTemp = [[NSMutableArray alloc]init];

self.eczaneler = [[NSMutableArray alloc] init];

Eczane *eczane1 = [[Eczane alloc] init];

eczane1.customerName = @"Duyarlı Eczanesi";

eczane1.customerID = @"mohammed@gmail.com";

eczane1.longitude = @"29.0312";
eczane1.latitude =  @"41.109817";

Eczane *eczane2 = [[Eczane alloc] init];

eczane2.customerName = @"Azim Eczanesi Azim";

eczane2.customerID = @"azim@gmail.com";

eczane2.longitude = @"30.0312";
eczane2.latitude =  @"41.109817";

[self.eczaneler addObject:eczane1];

[self.eczaneler addObject:eczane2];

Eczane *eczane3 = [[Eczane alloc] init];

eczane3.customerName = @"Optik Eczanesi";

eczane3.customerID = @"optik@gmail.com";

eczane3.longitude = @"29.0352";
eczane3.latitude =  @"40.109817";

[self.eczaneler addObject:eczane3];


for(int i=0; i<[eczaneler count];i++)
{

    Eczane *temp = [self.eczaneler objectAtIndex:i];


    CLLocationCoordinate2D coordinate;
    coordinate.longitude = [temp.longitude floatValue];
    coordinate.latitude = [temp.latitude floatValue];

    CustomAnnotation *point = [[CustomAnnotation alloc] init];
    point.coordinate = coordinate;


    [self.mapView addAnnotation:point];

}


}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{    
self.mapView.delegate = self;


[self.mapView setShowsUserLocation:YES];


return YES;

}



- (MKAnnotationView *)mapView:(MKMapView *)mapView2 viewForAnnotation:(id   <MKAnnotation>)annotation
{

if ([annotation isKindOfClass:[MKUserLocation class]])
{
    return nil;
}
else if ([annotation isKindOfClass:[CustomAnnotation class]])
{
    static NSString * const identifier = @"MyCustomAnnotation";


    MKAnnotationView* annotationView = [mapView2 dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView)
    {
        annotationView.annotation = annotation;
    }
    else
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:identifier];
    }

    // set annotationView properties

    UIImageView * ecz = [[UIImageView alloc]init];

    ecz.frame = CGRectMake(0, 0, 65, 49);

    ecz.image = [UIImage imageNamed:@"indicator.png"];


    UIImageView * vstd = [[UIImageView alloc]init];

    vstd.frame = CGRectMake(33, 10, 24, 22);

    vstd.image = [UIImage imageNamed:@"indicator_ziyaret_gri"];


    UIImageView * nbox = [[UIImageView alloc]init];

    nbox.frame = CGRectMake(48, -6, 22, 22);

    nbox.image = [UIImage imageNamed:@"numara_kutusu"];

    [annotationView addSubview:ecz];

    [annotationView addSubview:vstd];


    UILabel *index = [[UILabel alloc] initWithFrame:CGRectMake(5,4,15,15)];
  //  index.text =[NSString stringWithFormat:@"%d", iterator ];
    index.textColor = [UIColor whiteColor];
    [index setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18]];
    [nbox addSubview:index];
    [index setBackgroundColor:[UIColor clearColor]];




    [annotationView addSubview:nbox];

    //annotationView. = [UIImage imageNamed:@"indicator.png"];
    annotationView.canShowCallout = YES;

    return annotationView;
}

return nil;
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.tableViewController.tableView == self.searchDisplayController.searchResultsTableView) {

    return [filteredListContent count];

} else {
    return [eczaneler count];

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

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

 NSLog(@"cellForRowAtIndexPath");



 static NSString *CellIdentifier = @"EczaneCell";

 EczaneCell *cell = (EczaneCell *) [self.tableViewController.tableView dequeueReusableCellWithIdentifier:CellIdentifier];


 if (cell == nil) {
     cell = [[[EczaneCell alloc] initWithFrame:CGRectZero] autorelease];
 }

 Eczane *eczane = [[Eczane alloc]init];


 if (self.tableViewController.tableView == self.searchDisplayController.searchResultsTableView) {
     eczane = [self.filteredListContent objectAtIndex:[indexPath row]];
 } else {
     eczane = [self.eczaneler objectAtIndex:[indexPath row]];
 }

 cell.circleIndex.text        = [NSString stringWithFormat:@"%d", ([indexPath row]+1)];
 cell.cellName.text           = eczane.customerName;


 return cell;


 }


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//EczaneCell *cell = [tableView cellForRowAtIndexPath:indexPath];

Eczane *eczane = [self.eczaneler objectAtIndex:[indexPath row]];

CLLocationCoordinate2D coordinate;
    coordinate.longitude = [eczane.longitude floatValue];
    coordinate.latitude = [eczane.latitude floatValue];


MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);

[mapView setRegion:region animated:YES];

}



#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[filteredListContent removeAllObjects];

for (Eczane *eczObj in eczaneler)
{

    NSComparisonResult result = [eczObj.customerName compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
    if (result == NSOrderedSame)
    {
        [self.filteredListContent addObject:eczObj];
    }
}

[self.searchDispController.searchResultsTableView reloadData];

//[self.tableViewController.tableView reloadData];

//[self.tableViewController.tableView] = [self.searchDispController.searchResultsTableView ];

[self.tableViewController.tableView reloadData];

[searchDispController.searchResultsTableView setFrame:CGRectMake(0, 44, 320, [filteredListContent count]*(searchDispController.searchResultsTableView.rowHeight))];


return YES;
}



@end
4

1 回答 1

0

你的问题可能是:

contentsController:tableViewController

因为您已经从控制器中“窃取”了表视图并让自己成为委托和数据源,所以搜索控制器将尝试使用损坏的控制器收集源信息。

制作self内容控制器。

你的下一个问题是:

if (self.tableViewController.tableView == self.searchDisplayController.searchResultsTableView) {

同样,因为您破坏了该控制器(您应该修复它,为什么要创建表视图控制器然后窃取它的视图?)。将其更改为:

if (tableView == self.searchDisplayController.searchResultsTableView) {
于 2013-07-16T08:02:24.953 回答