草庐IT

objective-c - 在编辑模式下将 UISearchBar 与 UITableView 一起使用时,搜索结果不处于编辑模式

coder 2023-09-28 原文

我有一个 TableView Controller ,它实现了 UISearchBarDelegate 和 UISearchDisplayDelegate。我有一个按钮可以为我的表格 View 切换编辑 View 。当处于编辑模式时,用户可以选择多行并复制它们、删除它们等。如果我尝试在搜索栏中输入内容,而 TableView 处于编辑模式,shouldReloadTableForSearchString 方法会触发,然后我会重新 -使用更新的谓词从核心数据中获取我的数据,并从函数返回 YES,表示应重新加载 TableView 。当显示过滤结果时,它们不处于编辑模式,即使在 cellForRowAtIndexPath 中我可以看到 tableView isEditing 是 YES。我看不到多选的圆圈。在选择多个项目之前,我需要能够让用户过滤他们的列表,以便显着限制结果。

为什么结果没有显示在编辑模式中?

编辑模式,不使用搜索栏:

在编辑模式下搜索会产生非编辑模式且未设置样式的结果:

提前致谢, 亚历克斯

这里是 shouldReloadTableForSearchString 方法:

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
NSPredicate *predicate = nil;
if ([searchString length])
{
    NSArray* filteredTags = nil;
    if ([searchString length] < 3)
    {
        // Check for tags starting with 1 or 2 characters.
        filteredTags = [[tagResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(TagEntity* te, NSDictionary *bindings)
        {
            if ([[[te.name lowercaseString] substringToIndex:[searchString length]] isEqualToString:[searchString lowercaseString]])
                return YES;

            return NO;
        }]];

    }
    else
    {
        // Check for tags with 3 or more consecutive characters anywhere in name
        filteredTags = [[tagResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(TagEntity* te, NSDictionary *bindings)
        {
           if ([[te.name lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound)
               return YES;

           return NO;
        }]];
    }

    //  OR ANY(self.tags) in %@
    predicate = [NSPredicate predicateWithFormat:
        @"ANY(self.songlists) in %@ AND (name contains[cd] %@ OR artist_name contains[cd] %@ OR number = %@ OR ANY(tag_entities) in %@)",
        currentSonglist.songs, searchString, searchString, searchString,filteredTags];

    [self.fetchedResultsController.fetchRequest setPredicate:predicate];

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    // Return YES to cause the search result table view to be reloaded.

}

return YES;

这是 .h 文件中我的核心数据 TableView 的标题:

    @interface SonglistTVC : CoreDataTableViewController
<UISearchBarDelegate, 
UISearchDisplayDelegate,
UIPickerViewDataSource,
UIPickerViewDelegate,
UIActionSheetDelegate,
AddSongTVCDelegate,
ListSelectorDelegate>

我知道它正在使用我的 TableView Controller ,因为 cellForRowAtIndexPath 和 didSelectRowAtIndexPath 都在触发。我可以在内部进行调试,并且在 TableView 上将 isEditing 设置为 YES。但是 TableView 并没有反射(reflect)出它正确的编辑模式样式。

这里是 cellForRowAtIndexPath,它在搜索框中输入字母后立即触发。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = @"SongCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];

Song* song = [self.fetchedResultsController objectAtIndexPath:indexPath];     

UILabel* artistLabel    = (UILabel*)[cell viewWithTag:ARTIST_NAME_TAG];
UILabel* songLabel      = (UILabel*)[cell viewWithTag:SONG_NAME_TAG];
UILabel* keyLabel       = (UILabel*)[cell viewWithTag:KEY_TAG];
UILabel* numberLabel    = (UILabel*)[cell viewWithTag:NUMBER_TAG];

//artistLabel.font = [UIFont systemFontOfSize:17];
[artistLabel setText:   song.artist_name];
[songLabel setText:     song.name];
//[songLabel setFont:[UIFont systemFontOfSize:16]];
[keyLabel setText:      song.key];
[numberLabel setText:   [NSString stringWithFormat:@"%@", song.number]];


UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,61)];
UIView *selectedBackView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,61)];

CAGradientLayer *backGradient = [[CAGradientLayer alloc] init];
backGradient.frame = cell.layer.bounds;

CAGradientLayer *selectedGradient = [[CAGradientLayer alloc] init];
selectedGradient.frame = cell.layer.bounds;

if (song.status == [NSNumber numberWithUnsignedInt: 1])
{
    [backGradient setColors:[NSArray arrayWithObjects:
                           (id)[[UIColor colorWithRed:240.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],
                           (id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],
                           nil]];
}
else if (song.status == [NSNumber numberWithUnsignedInt: 2])
{
    [backGradient setColors:[NSArray arrayWithObjects:
                         (id)[[UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],
                         (id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],
                         nil]];
}
else if (song.status == [NSNumber numberWithUnsignedInt: 3])
{
    [backGradient setColors:[NSArray arrayWithObjects:
                         (id)[[UIColor colorWithRed:200.0f/255.0f green:240.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],
                         (id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],
                         nil]];
}
[selectedGradient setColors:[NSArray arrayWithObjects:
                     (id)[[UIColor colorWithRed:255.0f/255.0f green:160.0f/255.0f blue:60.0f/255.0f alpha:1.0f] CGColor],
                     (id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:0.7f] CGColor],
                     nil]];


if (![self.tableView isEditing] )
{
    cell.backgroundView = backView;
    [cell.backgroundView.layer insertSublayer:backGradient atIndex:1];

    cell.selectedBackgroundView = selectedBackView;
    [cell.selectedBackgroundView.layer insertSublayer:selectedGradient atIndex:1];
}
else
{
    //cell.backgroundView = backView;
    //[cell.backgroundView.layer insertSublayer:backGradient atIndex:1];
    cell.selectedBackgroundView = backView;
    [cell.selectedBackgroundView.layer insertSublayer:backGradient atIndex:1];
}

for (NSIndexPath* ip in selectedIndexes)
{
    [self.tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
}

[sortOrderPicker reloadAllComponents];
[self sendPickerDownAnimated:NO];

return cell;

我使用分段控件内的按钮来打开/关闭编辑:

    - (IBAction)segmentChanged:(id)sender{
UISegmentedControl* sg = (UISegmentedControl*)sender;
if (sg.selectedSegmentIndex == 0)
{
    // Edit
    if (![self.tableView isEditing])
    {
        [self.tableView setEditing:YES animated:YES];
        [self.navigationController setToolbarHidden:NO animated:YES];
    }
    else
    {
        [self.tableView setEditing:NO animated:YES];
        [self.navigationController setToolbarHidden:YES animated:YES];
        selectedIndexes = nil;
    }
    [self.tableView reloadData];
}
else
{
    // Sort
    if (pickerShown)
        [self sendPickerDownAnimated:YES];
    else
        [self bringPickerUpAnimated:YES];
}

最佳答案

问题在于 UISearchDisplayController 会自动为您做很多事情。它创建自己的 TableView 来显示搜索结果,并管理该 TableView 。您可以将自己设置为该 TableView 的数据源和委托(delegate),默认情况下会自动为您完成,但这不是您的 TableView ,您无法控制它。如果你想要更好的方法是指定这是什么 TableView ,你需要设置搜索显示 Controller 的 searchResultsTableView;或者您可以在事情进行时获得对它的引用(例如,通过 UISearchDisplayController 自己的委托(delegate)方法)。但即便如此,您也不会成为管理结果 TableView 的 UITableViewController;这完全是 UISearchDisplayController 内部的。如果您不喜欢那样,一个简单的解决方案是:不要使用 UISearchDisplayController - 考虑另一个界面。

关于objective-c - 在编辑模式下将 UISearchBar 与 UITableView 一起使用时,搜索结果不处于编辑模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14151133/

有关objective-c - 在编辑模式下将 UISearchBar 与 UITableView 一起使用时,搜索结果不处于编辑模式的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  8. ruby-on-rails - 如果我将 ruby​​ 版本 2.5.1 与 rails 版本 2.3.18 一起使用会怎样? - 2

    如果我使用ruby​​版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby​​1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更

  9. ruby - 是否有用于序列化和反序列化各种格式的对象层次结构的模式? - 2

    给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最

  10. c - mkmf 在编译 C 扩展时忽略子文件夹中的文件 - 2

    我想这样组织C源代码:+/||___+ext||||___+native_extension||||___+lib||||||___(Sourcefilesarekeptinhere-maycontainsub-folders)||||___native_extension.c||___native_extension.h||___extconf.rb||___+lib||||___(Rubysourcecode)||___Rakefile我无法使此设置与mkmf一起正常工作。native_extension/lib中的文件(包含在native_extension.c中)将被完全忽略。

随机推荐