HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在搜索过程中,ios – UITableViewCell的详细信息按钮按钮太远大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有UITableViewController与一个UISearchBar作为tableView的tableHeaderView.我也有一个UISearchDisplayController初始化为UISearchBar作为其searchBar和UITableViewController作为其内容控制器.到目前为止这么好,一切都有效.

问题是UITableView有一些单元格,它们的“附件类型”设置为UITableViewCellAccessoryDe​​tailDisclosureButton.发生这种情况:

开始,一切看起来应该是:
>用户点击UISearchBar.
> UISearchDisplayController在主表的顶部创建黑色叠加层,并使主表的索引(as,sectionIndextitlesForTableView)消失.
>假设用户此时隐藏键盘(按下标准键盘上的iPad“隐藏键盘”按钮)
>由于用户还没有在UISearchBar中输入任何内容,我们仍然可以看到主表,尽管在UISearchDisplayController添加的黑色叠加层之下.
>键盘的隐藏暴露了更多的主表,导致主表加载更多的单元格.
>现在这里的问题是:由于这些单元格在主表的索引被隐藏时加载,所以显示的按钮显示得太过分(至少与其他单元格相比).
>此外,当用户现在取消搜索时,可能不会重新加载这些单元,导致公开按钮被显示在索引下面(现在再次可见).

我正在努力解决这个问题;我可以想到的唯一选择是找到对应于公开按钮的UIView,并手动移动它,但是这似乎是非常可笑的,如果只是因为甚至找到UIView requires a nasty hack.关于如何以更好的方式解决这个问题的任何建议非常感谢!

最小的可运行示例

以下是一个最小的例子.只需启动一个新的XCode项目,仅启用ARC,iPad,并将AppDelegate的内容替换为以下内容.请注意,为了最小化的示例,我强制主表在searchDisplayController中重新加载单元:willShowSearchResultsTableView,否则主表将缓存其单元格,并且问题不会显示(在我的实际应用程序中,主表正在重新加载其单元格对于其他原因,我不完全确定为什么 – 但是当然,主表可以随时重新加载单元格.)

要查看问题发生,请运行代码,在搜索框中输入内容(您将看到“搜索结果0 .. 5”),然后取消搜索.主表的公开按钮现在显示在索引下方而不是旁边.

下面只是代码

@interface AppDelegate ()

@property (nonatomic,strong) UITableViewController* mainTableController;
@property (nonatomic,strong) UISearchDisplayController* searchDisplay;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    UITableViewController* tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];

    UITableView* tableView = [tableViewController tableView];
    [tableView registerClass:[UITableViewCell class] forCellReusEIDentifier:@"Cell"];
    [tableView setDatasource:self];
    [self setMainTableController:tableViewController];

    UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,44)]; // Width set automatically
    [tableView setTableHeaderView:searchBar];

    UISearchDisplayController* searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
                                                                                 contentsController:tableViewController];
    [searchDisplay setSearchResultsDatasource:self];
    [searchDisplay setDelegate:self];
    [self setSearchDisplay:searchDisplay];

    [[self window] setRootViewController:tableViewController];

    self.window.BACkgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView != [[self searchDisplay] searchResultsTableView]) {
        return 26;
    } else {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView != [[self searchDisplay] searchResultsTableView]) {
        return 10;
    } else {
        return 5;
    }
}

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
    // The problem arises only if the main table view needs to reload its data
    // In this minimal example,we force this to happen
    [[[self mainTableController] tableView] reloadData];

    [tableView registerClass:[UITableViewCell class] forCellReusEIDentifier:@"SearchCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath {
    if (tableView != [[self searchDisplay] searchResultsTableView]) {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

        [[cell textLabel] setText:[NSString StringWithFormat:@"%c%d",'A' + [indexPath section],[indexPath row]]];
        [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];

        return cell;
    } else {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];

        [[cell textLabel] setText:[NSString StringWithFormat:@"Search result %d",[indexPath row]]];
        [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];

        return cell;
    }
}

- (NSArray *)sectionIndextitlesForTableView:(UITableView *)tableView {
    if (tableView != [[self searchDisplay] searchResultsTableView]) {
        return [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
    } else {
        return nil;
    }
}

解决方法

如果您想要模仿苹果自己的应用程序在这些情况下似乎表现的方式,那么正确的操作过程将是开始搜索时所有向右移动的详细信息按钮的原因,然后再次重新移动一次搜索完成.

我在你的例子中已经实现了这一点,通过在主表中查看两个UISearchDisplayDelegate方法,我添加你的代码调用reloadData:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    [[[self mainTableController] tableView] reloadData];
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    [[[self mainTableController] tableView] reloadData];
}

这将强制您的详细披露视图重新定位,以表视图索引的可见性,保持所有披露指标的位置彼此一致,无论是否在搜索模式.

更新

我在其他UISearchDisplayDelegate方法中重新加载表视图,包括

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
- (void)searchDisplayController:(UISearchDisplayController *)controller willUnloadSearchResultsTableView:(UITableView *)tableView

但是这些产生了一个相当刺耳的效果,其中详细的公开按钮的位置突然跳出来,所以我建议如前所述的willBeginSearch和willEndSearch方法.

大佬总结

以上是大佬教程为你收集整理的在搜索过程中,ios – UITableViewCell的详细信息按钮按钮太远全部内容,希望文章能够帮你解决在搜索过程中,ios – UITableViewCell的详细信息按钮按钮太远所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。