我有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的详细信息按钮按钮太远的更多相关文章

  1. html5教你做炫酷的碎片式图片切换 (canvas)

    本篇文章主要介绍了html5教你做炫酷的碎片式图片切换 (canvas) ,具有一定参考价值,有兴趣的可以了解一下

  2. ios – UITableView和Cell Reuse

    这是我的CustomCell类的init方法解决方法如果没有要显示的图像,则必须清除图像视图:

  3. ios – fetchedResultsController.fetchedObjects.count = 0但它充满了对象

    我正在使用相当标准的fetchedResultsController实现来输出tableView.在-viewDidLoad的最后,我正在进行第一次调用:这是我的fetchedResultsController:我的tableView方法:所以,问题是:在_fetchedResultsController.fetchedobjects.count的日志中等于0,但在视觉上tableView充满了对

  4. ios – 如何将UICollectionViewCell从一个UICollectionView拖到另一个UICollectionView?

    如果是这样,我将如何实施它?

  5. ios – UITableView在滚动时阻止重新加载

    或者你能想象一个防止这种行为的好方法吗?解决方法抱歉,我没有足够的声誉来添加评论,因此在单独的答案中回答您的上一个问题.-performSelector:withObject:afterDelay:延迟为0.0秒不会立即执行给定的选择器,而是在当前的RunloopCycle结束后和给定的延迟之后执行它.-performSelector:withObject:添加到当前Runloop循环中并执行.这与直接调用该方法相同.因此,使用-performSelector:withObject:afterDelay:

  6. xcode – 在自定义表视图单元格中嵌入集合视图

    我有一个故事板的图像,你可以看到自定义表格单元格然后底部是一个集合视图,我想填充图像–只是不知道如何?我也不确定哪些信息可能会有所帮助,所以如果有信息遗失,我很抱歉.解决方法您应该将UICollectionView的Delagate和DataSource放在自定义UITableViewCell类中.这是一个nicetutorial.它是关于tableview单元格中的tableview,但这个想法非常相似.祝好运!

  7. ios – 在Swift中通过标记访问UITableViewCell内部的不同视图

    我正在尝试使用swift为iOS8制作应用程序.这里的目标是制作一种新闻源.此Feed显示来自用户的帖子,其遵循特定模式.我想过使用UITableView,其中每个单元格都遵循自定义布局.当我尝试访问其中的文本标签时出现问题.我尝试通过它的标签访问它,但是当我这样做时,整个应用程序崩溃了.报告的错误是“Swift动态转换失败”,我使用以下代码访问视图:难道我做错了什么?解决方法我认为问题是标签0.所有视图都是默认值0.所以尝试另一个标签值.

  8. ios – 如何实现`prepareForReuse`?

    解决方法尝试将此添加到您的MGSwipeTableCell.m:

  9. ios – 使用动态单元格高度时,将表格视图滚动到底部

    使用动态单元格高度时,如何将表格视图滚动到底部?出于某种原因,此代码在此方案中不起作用:谢谢!

  10. ios – 在UITableView上轻扫以删除以使用UIPanGestureRecognizer

    我使用以下代码将UIPanGuestureRecognizer添加到整个视图中:在主视图中我有一个UITableView,它有这个代码来启用滑动删除功能:只有RUNNING1打印到日志中,并且“删除”按钮不会显示.我相信其原因是UIPanGestureRecognizer,但我不确定.如果这是正确的,我该如何解决这个问题.如果这不正确,请提供原因并解决.谢谢.解决方法从document:Ifage

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部