应用搜索功能中的UI问题(详细视图与表格视图)

时间:2012-12-03 16:06:23

标签: objective-c search xcode4.5 uisearchdisplaycontroller

请有人查看并修复我的代码。我已经玩了很多年了,似乎无法做到这一点。我修好一部分而另一部分停止工作!我正在努力让搜索工作继续保持部分标题。然后从原始表格视图和搜索结果中推送到详细视图。我目前的代码是:

#import "SearchWillWorkViewController.h"
#import "SearchDetailViewController.h"

@interface SearchWillWorkViewController ()

@end

@implementation SearchWillWorkViewController

@synthesize mainTableView;
@synthesize contentsList;
@synthesize searchResults;
@synthesize savedSearchTerm;
@synthesize stoneSections;
@synthesize detailViewController = _detailViewController;

- (void)viewDidUnload
{
    [super viewDidUnload];

    [self setSavedSearchTerm:[[[self searchDisplayController] searchBar] text]];

    [self setSearchResults:nil];
}

- (void)viewDidLoad
{   
    [super viewDidLoad];
    [self createStoneData];

    if ([self savedSearchTerm])
    {
        [[[self searchDisplayController] searchBar] setText:[self savedSearchTerm]];
    }
}

- (void)viewWillAppear:(BOOL)animated
{   
    [super viewWillAppear:animated];

    [[self mainTableView] reloadData];
}

- (void)createStoneData {

    self.stoneSections=[[NSArray alloc] initWithObjects:
                        @"Granite",@"Marble",@"Quartz",@"Silestone",nil];

    graniteArray = [[NSMutableArray alloc] init];
    marbleArray = [[NSMutableArray alloc] init];
    quartzArray = [[NSMutableArray alloc] init];
    silestoneArray = [[NSMutableArray alloc] init];

    [graniteArray addObject:[[NSMutableDictionary alloc]
                             initWithObjectsAndKeys:@"Angel Cream", @"name",
                             @"angel-cream.jpg", @"image", nil]];
    [graniteArray addObject:[[NSMutableDictionary alloc]
                             initWithObjectsAndKeys:@"Angola Black", @"name" ,
                             @"angola_black.jpg", @"image" , nil]];


    [marbleArray addObject:[[NSMutableDictionary alloc]
                            initWithObjectsAndKeys:@"Arabescato", @"name",
                            @"arabescato.jpg", @"image", nil]];
    [marbleArray addObject:[[NSMutableDictionary alloc]
                            initWithObjectsAndKeys:@"Bianco Carrara", @"name",
                            @"bianco-carrara.jpg", @"image", nil]];


    [quartzArray addObject:[[NSMutableDictionary alloc]
                            initWithObjectsAndKeys:@"Caesarstone: Black Knight", @"name",
                            @"Black Knight.jpg", @"image", nil]];
    [quartzArray addObject:[[NSMutableDictionary alloc]
                            initWithObjectsAndKeys:@"Caesarstone: Black Rocks", @"name",
                            @"Black Rocks.jpg", @"image", nil]];


    [silestoneArray addObject:[[NSMutableDictionary alloc]
                               initWithObjectsAndKeys:@"Cielo: Aluminio Nube", @"name",
                               @"silestone-aluminio-nube.jpg", @"image", nil]];
    [silestoneArray addObject:[[NSMutableDictionary alloc]
                               initWithObjectsAndKeys:@"Cielo: Blanco Dune", @"name",
                               @"silestone-blanco-dune.jpg", @"image", nil]];


    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:graniteArray,marbleArray,quartzArray,silestoneArray, nil];
    [self setContentsList:array];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
}

- (void)handleSearchForTerm:(NSString *)searchTerm
{
    [self setSavedSearchTerm:searchTerm];

    if ([self searchResults] == nil)
    {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [self setSearchResults:array];
    }

    [[self searchResults] removeAllObjects];

    if ([[self savedSearchTerm] length] != 0)
    {
        for (NSMutableArray *array in contentsList)
        {
            for (NSDictionary* dictionary in array)
            {
                NSString *currentstring = [dictionary objectForKey:@"name"];
                NSRange r = [currentstring rangeOfString:searchTerm options:NSCaseInsensitiveSearch];
                if (r.location != NSNotFound) {
                    [[self searchResults] addObject:currentstring];
                }
            }
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        return [self.stoneSections count];
    else
        return [self.stoneSections count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.stoneSections objectAtIndex:section];
}

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

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        rows = [[self searchResults] count];
    else
        rows = [[self.contentsList objectAtIndex:section] count];


    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *contentForThisRow = nil;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        contentForThisRow = self.searchResults [indexPath.row];
    else
        contentForThisRow = [[[[self contentsList] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];

    static NSString *CellIdentifier = @"stoneCell";

    UITableViewCell *cell = nil;
    if(tableView == [[self searchDisplayController] searchResultsTableView]){
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.textLabel.text = self.searchResults [indexPath.row];
    } else {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.text = [[[self.contentsList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.detailViewController.detailItem=[[contentsList
                                           objectAtIndex:indexPath.section]
                                          objectAtIndex: indexPath.row];    
}

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

    return YES;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    [self setSavedSearchTerm:nil];

    [[self mainTableView] reloadData];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    self.detailViewController=segue.destinationViewController;
}

@end

当我搜索它时会在所有部分中显示第一个结果,并且不会进入详细视图。搜索前的原始表视图确实推送视图但没有单元格标题,搜索后如果我点击取消它再次崩溃。基本上它是一个真正的混乱,我不知道如何解决它,我已经尝试了一切,仍然无法完成它!

我并没有要求天才来取悦我的痛苦并纠正这种破碎的编码。

非常感谢提前!

0 个答案:

没有答案