0

我尝试制作一个小程序来了解有关搜索关于搜索栏的 tableview 的更多信息。与此同时,我正在第三周尝试这样做。我在互联网上以不同的方式找到了许多示例。最多使用搜索显示控制器,但这种方式仅适用于视图控制器。我更喜欢一种也适用于 uiview 的方式,因此我使用搜索栏委托方法来过滤我的数据字典。通常我会尝试自己处理这些事情。但这是我的克星。我真的找不到解决这个问题的方法。任何人都可以帮我处理我的代码吗?这里是。

#import "FilterDemoTableViewController.h"

@implementation FilterDemoTableViewController

@synthesize filteredTableData;
@synthesize searchBar;
@synthesize isFiltered;

@synthesize tableContents;
@synthesize Keys;


- (void)viewDidLoad
{
    [super viewDidLoad];

    searchBar.delegate = (id)self;


    //-----------------------My TableView Data ------------------------------

    NSArray *array1 = [[NSArray alloc]initWithObjects:@"Berlin",@"München",@"Stuttgart",nil];
    NSArray *array2 = [[NSArray alloc]initWithObjects:@"Paris",@"Bordeaux",@"Marseille",@"Toulouse",nil];
    NSArray *array3 = [[NSArray alloc]initWithObjects:@"London",@"Portsmouth",@"Oxford",@"York",@"Dover",nil];
    NSArray *array4 = [[NSArray alloc]initWithObjects:@"Rom" ,@"Genua",@"Mailand",@"Florenz",nil];
    NSArray *array5 = [[NSArray alloc]initWithObjects:@"Madrid",@"Barcelona",@"Toledo",@"Saragossa",@"Pamplona",nil];
    NSDictionary *dictionary =[[NSDictionary alloc]initWithObjectsAndKeys:array1,@"Deutschland",array2,@"Frankreich",array3,@"Großbritannien",array4,@"Italien",array5,@"Spanien",nil];

    self.tableContents = dictionary;
    self.Keys = [self.tableContents allKeys];

    //--------------------------------------------------------------------------    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.isFiltered) {
        return [filteredTableData count];
    } else {
        return [Keys count];}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{   

    NSArray *listData =[self.tableContents objectForKey:[self.Keys objectAtIndex:section]];

    int rowCount;
    if(self.isFiltered)
        rowCount = filteredTableData.count;
    else
        rowCount = [listData count];

    return rowCount;

}


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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];



    NSDictionary* sectionDictionary;

    if (isFiltered) {
        sectionDictionary = [filteredTableData objectAtIndex:indexPath.section];
    } else {
        sectionDictionary = [self.tableContents objectForKey:[self.Keys objectAtIndex:indexPath.section]];
    }

    NSArray* sectionEntries = [self.tableContents objectForKey:[self.Keys objectAtIndex:indexPath.section]];

    cell.textLabel.text = [sectionEntries objectAtIndex:indexPath.row]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}



-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
    if(text.length == 0)
    {
        isFiltered = FALSE;
    }
    else
    {
     //I think here is something wrong but i don't know what

        isFiltered = true;

        filteredTableData = [[NSMutableArray alloc] init];

        NSMutableArray *searchArray = [[NSMutableArray alloc] init];

        for (NSDictionary *dictionary in tableContents)    //dictionary read
        {
            NSArray *array = [dictionary objectForKey:Keys];  //section of dictionary read
            [searchArray addObjectsFromArray:array];         
        }

        for (NSString *sTemp in searchArray)    
        {
            NSRange titleResultsRange = [sTemp rangeOfString:text options:NSCaseInsensitiveSearch];

            if (titleResultsRange.length != 0)         
                [filteredTableData addObject:sTemp];
        }
    }
    [self.tableView reloadData];
}



- (void)viewDidUnload{
    [self setSearchBar:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end
4

1 回答 1

0

现在我发布一个完整的刷新代码。我逐步测试了代码的每个组件。它可以工作并且搜索也很好(在 UITableViewController 中)。但在本例中,漏洞代码位于 UIView 中。原因是,通过这种方式可以创建非全尺寸的表格视图和表格视图的实例。对于清晰的 Viewcontroller 来说要好得多。我知道 UIView 没有类似reloadData的方法,但这是必要的。在代码末尾附近,您可以看到问题行。对于这最后一步,我不知道如何解决这个问题。[self.tableView reloadData];

#import "TableView.h"
#import <QuartzCore/QuartzCore.h>

@interface TableView ()

@end

@implementation TableView
@synthesize delegate;
@synthesize dropDownHeight;
@synthesize labelText;
@synthesize enabled;

@synthesize tableContents;
@synthesize Keys;

@synthesize searchBar;
@synthesize isFiltered;
@synthesize filteredTableData;


- (void)__show {
   viewControl.alpha = 0.0f;
   UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
   [mainWindow addSubview:viewControl];
   [UIView animateWithDuration:0.3f
                 animations:^{
                     viewControl.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {}];
}
- (void)__hide {
   [UIView animateWithDuration:0.2f
                 animations:^{
                     viewControl.alpha = 0.0f;
                 }
                 completion:^(BOOL finished) {
                     [viewControl removeFromSuperview];
                 }];
}
- (void) setLabelText:(NSString *)_labelText{
   [button setTitle:labelText forState:UIControlStateNormal];
}
- (void) setEnable:(BOOL)_enabled{
   enabled = _enabled;
   [button setEnabled:_enabled];
}
- (void) setArrayData:(NSArray *)_arrayData{
   [table reloadData];
}
- (void) buttonPressed{
   [self __show];
}
- (void) controlPressed{
 //[viewControl removeFromSuperview];
   [self __hide];
}


- (id) initWithFrame:(CGRect)frame
{

self = [super initWithFrame:frame];
if (self) {
    // Initialization code
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(10, 0, 280, 30)];
    [button setTitle:@"--Auswahl--" forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"combo_bg.png"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:14]];
    [self addSubview:button];
    dropDownHeight = 706;

    viewControl = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [viewControl addTarget:self action:@selector(controlPressed) forControlEvents:UIControlEventTouchUpInside];

    CGFloat x = self.frame.origin.x;
    CGFloat y = (viewControl.frame.size.height - dropDownHeight)/2;

    table = [[UITableView alloc] initWithFrame:CGRectMake(x, y, frame.size.width, dropDownHeight) style:UITableViewStyleGrouped];
    table.dataSource = self;
    table.delegate = self;

    searchBar = [[UISearchBar alloc] init];
    [searchBar sizeToFit];
    searchBar.delegate = (id)self;
    table.tableHeaderView = searchBar;

    CALayer *layer = table.layer;
    layer.masksToBounds = YES;
    layer.cornerRadius = 2.0f;
    layer.borderWidth = 1.0f;
    [layer setBorderColor:[UIColor darkGrayColor].CGColor];
    [viewControl addSubview:table];

    CGAffineTransform rotateTable = CGAffineTransformMakeRotation(M_PI_2);
    table.transform = rotateTable;
    table.frame = CGRectMake(-2, -1, table.frame.size.width, table.frame.size.height);

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradientBackground.png"]];
    [tempImageView setFrame:self->table.frame]; 
    table.backgroundView = tempImageView;


    //----------------------- TableView Daten ------------------------------

    NSArray *array1 = [[NSArray alloc]initWithObjects:@"Berlin",@"München",@"Stuttgart",@"Hamburg",nil];
    NSArray *array2 = [[NSArray alloc]initWithObjects:@"Paris",@"Bordeaux",@"Marseille",@"Toulouse",nil];
    NSArray *array3 = [[NSArray alloc]initWithObjects:@"London",@"Portsmouth",@"Oxford",@"York",@"Dover",nil];
    NSArray *array4 = [[NSArray alloc]initWithObjects:@"Rom" ,@"Genua",@"Mailand",@"Florenz",nil];
    NSArray *array5 = [[NSArray alloc]initWithObjects:@"Madrid",@"Barcelona",@"Toledo",@"Saragossa",@"Pamplona",nil];
    NSDictionary *dictionary =[[NSDictionary alloc]initWithObjectsAndKeys:array1,@"Deutschland",array2,@"Frankreich",array3,@"Großbritannien",array4,@"Italien",array5,@"Spanien",nil];

    self.tableContents = dictionary;
    self.Keys = [self.tableContents allKeys];

    // ---------------------------------------------------------------------
  }
return self;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([self tableView:tableView titleForHeaderInSection:section] != nil) {
    return 40;
}
else {
    return 0;
}
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section      {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
    return nil;
}

// Create label with section title
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(20, 6, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;

// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
[view addSubview:label];

return view;
}


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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section {

return [self.Keys objectAtIndex:section];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.isFiltered)
{
    return 1;
} else {
    return [Keys count];}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{   
int rowCount;
if(self.isFiltered)
{
    rowCount = [filteredTableData count];
}
else
{
    NSArray *listData =[self.tableContents objectForKey:[self.Keys objectAtIndex:section]];
    rowCount = [listData count];
}
return rowCount;

}


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

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

if(cell == nil) {

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
}

if (isFiltered)
{
    NSString * stadt  = [filteredTableData objectAtIndex:indexPath.row];
    cell.textLabel.text = stadt;
}
else
{
    NSDictionary* sectionDictionary;
    sectionDictionary = [self.tableContents objectForKey:[self.Keys objectAtIndex:indexPath.section]];
    NSArray* sectionEntries = [self.tableContents objectForKey:[self.Keys objectAtIndex:indexPath.section]];
    cell.textLabel.text = [sectionEntries objectAtIndex:indexPath.row];
}

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSArray *listData =[self.tableContents objectForKey:[self.Keys objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
[tableView deselectRowAtIndexPath:indexPath animated:YES];

selectedIndex = [indexPath row];

[self __hide];
[button setTitle:[[NSString alloc] initWithFormat:rowValue] forState:UIControlStateNormal];

}


- (NSInteger) selectedIndex {

return selectedIndex;
}


-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
    isFiltered = FALSE;
}
else
{
    isFiltered = true;
    [filteredTableData removeAllObjects];

    for (NSString* key in self.tableContents)
    {

        NSArray *staedte = [self.tableContents objectForKey:key];

        for (NSString *stadt in staedte)
        {

            NSRange titleResultsRange = [stadt rangeOfString:text options:NSCaseInsensitiveSearch];

            if (titleResultsRange.length != 0)
            {
                [filteredTableData addObject:stadt];
            }
        }
    }
}
[self.tableView reloadData];  //Here is the error
}



-(void)didChangeTableViewValue:(TableView *)TableView selectedIndex:(NSInteger)selectedIndex {

}


@end
于 2012-09-14T21:45:39.443 回答