iOS7中的UISearchBar和UITableView问题 [英] Issue with UISearchBar and UITableView in iOS7

查看:106
本文介绍了iOS7中的UISearchBar和UITableView问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在iOS6中运行良好的应用程序。它有一个带有搜索栏的表格视图。当我在iOS7中运行它时,我遇到了以下问题:

I have an app that works fine in iOS6. It has a table view with a search bar. When I run it in iOS7 I got the following issue:

正如您在上图所示,搜索结果显示在错误的位置,它们重叠搜索控制,任何想法如何解决这个问题?

As you can see in the image above, the search results are displayed in a wrong position, they are overlapping the search control, any idea how to fix this?

第一张图片显示搜索控件,搜索结果应显示在第一张图片中红色标记的位置。

The first image is showing the search control, and the search results should be shown in the position I marked in red in that first image.

谢谢。 -Fernando

Thanks. -Fernando

好吧,我做了一些改动,但仍然不太好:

Well, I made some changes but it is still not so good:

-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
       if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
            // The tableView the search tableView replaces
            CGRect f = self.searchFavoriteListTable.frame;
            CGRect f1 = tableView.frame;

            //CGRect s = self.searchDisplayController.searchBar.frame;
            CGRect updatedFrame = CGRectMake(f1.origin.x,
                                             f.origin.y + 45,
                                             f1.size.width,
                                             f1.size.height - 45);

            tableView.frame = updatedFrame;
        }
}

我要删除的是红色部分最后一张图片......它与其他视图重叠。

What I want to remove is the red part in the last image... it is overlapping other view.

推荐答案

第一步,创建一般格式和一般框架的搜索栏;

First step, create a searchbar with general format and general frame;

UISearchBar *mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[mySearchBar sizeToFit]; // if you give it CGRectZero and sizeToFit, it will shown exactly end of the your navigationBar.

mySearchBar.tintColor = [UIColor whiteColor];
mySearchBar.placeholder = @"Search Music";
mySearchBar.showsScopeBar = NO;
mySearchDisplayController *mySearchDisplay = [[mySearchDisplayController alloc] mySearchBar contentsController:self];

然后创建一个新的类类型UISearchDisplayController作为名称mySearchDisplayController,如您所见,我们应该将你的搜索栏合并到其中3行。不要忘记为你的新类实现UITableView协议;

Then create a new class type of "UISearchDisplayController" as a name "mySearchDisplayController" and as you see, we should merge your searchbar in it 3 lines up. Don't forget implement UITableView protocol to your new class like that;

@interface mySearchDisplayController : UISearchDisplayController <UISearchDisplayDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>

然后在你的新mySearchDisplayController类中实现该方法;

Then in your new mySearchDisplayController class implement that method;

    -(void)searchDisplayControllerWillBeginSearch:(mySearchDisplayController *)controller
{
    self.searchResultsDataSource = self;
    self.searchResultsTableView.delegate = self;
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
    {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        [UIView animateWithDuration:0.01 animations:^{
            for (UIView *subview in self.searchBar.subviews)
                subview.transform = CGAffineTransformMakeTranslation(0, statusBarFrame.size.height);
        }];
    }
}

-(void)searchDisplayControllerWillEndSearch:(mySearchDisplayController *)controller
{
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
    {
        [UIView animateWithDuration:0.01 animations:^{
            for (UIView *subview in self.searchBar.subviews)
                subview.transform = CGAffineTransformIdentity;
        }];
    }
}

最后一步,你应该确定你的新帧结束搜索栏到你的tableview;

And last step, you should identify your new frame end of the searchbar to your tableview;

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSInteger)scope
{
        [self.filteredSearchResults removeAllObjects]; // First clear the filtered array.
            for(NSDictionary *searchResult in // your search array)
            {
                NSString *searchableString = [NSString stringWithFormat:@"%@ %@", [searchResult objectForKey:@"//your search key"]];
                NSRange stringRange = [searchableString rangeOfString:searchText options:NSCaseInsensitiveSearch];
            }
    }
    [self.searchResultsTableView reloadData];

    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenHeight = screenSize.height;
    CGRect frame = self.searchResultsTableView.frame;

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
    {
        frame.size.height = screenHeight-64.0f;
    }
    else
    {
        frame.size.height = screenHeight-44.0f;
    }
    self.searchResultsTableView.frame = frame;
}

我2个月前为我的应用编写了该代码,它适用于iOS 7&& 6&&希望它有效。

I wrote that code 2 months ago for my app and it works perfect for iOS 7 && 6 && 5. Hope it works.

这篇关于iOS7中的UISearchBar和UITableView问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆