- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含多个 NSString 元素的数组,还有一个 View ,其中有一个搜索栏。我想开始搜索用户在搜索字段中写入的内容作为每个 NSString 元素的子字符串。如果 NSString 元素包含子字符串,我想将其添加到新数组中。我尝试用代码来做到这一点,它工作正常,但它不断添加对象,即使它已经为它们添加了对象,所以有人知道为什么吗?我已经将委托(delegate)设置为 self。
//.h @接口(interface)类1
@property(strong, nonatomic) NSMutableArray *allExercisesNames;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property(strong, nonatomic) NSMutableArray *foo;
@end
//.m
@implementation class1
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
for(int i = 0; i < _allExercisesNames.count;i++)
{
if([[_allExercisesNames objectAtIndex:i] rangeOfString:searchBar.text].location != NSNotFound)
{
if(_foo == nil)
{
_foo = [[NSMutableArray alloc]initWithObjects:[_allExercisesNames objectAtIndex:i], nil];
[self.gridView reloadData];
}
else
{
[_foo addObject:[_allExercisesNames objectAtIndex:i]];
[self.gridView reloadData];
}
}
}
if([searchBar.text isEqualToString:@""] || searchBar.text==nil)
//![_allExercisesNames containsObject:searchBar.text
{
_foo = nil;
[self.gridView reloadData];
}
}
最佳答案
您没有在函数顶部将“nil”分配给 _foo,因此每次您键入内容时,它都会继续添加到旧的 _foo 数组中。
相反,您应该将 _foo 设置为函数顶部的一个新数组,以便在扫描 _allExercisesNames 数组之前将其清除。
这是一个固定的简化版本,它还使用“for (x in y)”来使其更加简洁。
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if([searchBar.text isEqualToString:@""] || searchBar.text==nil)
_foo = nil;
else
{
_foo = [NSMutableArray new];
for(NSString *exercise in _allExercisesNames)
if([exercise rangeOfString:searchBar.text].location != NSNotFound)
[_foo addObject:exercise];
}
[self.gridView reloadData];
}
关于objective-c - 在 NSString 搜索栏中不断搜索子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10270148/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!