Kev*_*tre 1 iphone cocoa-touch ipad
我有一个UITableView包含我想要按第一个字母分组(和排序)的名称(类似于地址簿应用程序).我目前能够匹配任何部分('A' - 'Z')使用:
// Sections is an array of strings "{search}" and "A" to "Z" and "#".
NSString *pattern = [self.sections objectAtIndex:section];
NSPredicate *predicate = nil;
// Ignore search pattern.
if ([pattern isEqualToString:@"{search}"]) return nil;
// Non-Alpha and Non-Diacritic-Alpha (?).
if ([pattern isEqualToString:@"#"]);
// Default case (use case and diacritic insensitivity).
if (!predicate) predicate = [NSPredicate predicateWithFormat:@"name beginswith[cd] %@", pattern];
// Return filtered results.
return [self.friends filteredArrayUsingPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
但是,"#"的匹配使我失望.我尝试使用以下方法构建REGEX匹配:
[NSPredicate predicateWithFormat:@"name matches '[^a-zA-Z].*'"];
Run Code Online (Sandbox Code Playgroud)
但这对于变音符号-α失败(出现重复的行).任何想法将不胜感激!谢谢.
我通常使用UILocalizedIndexedCollation和自定义NSArray类别来处理这个问题.我还有一个UILocalizedIndexedCollation的包装器(装饰器),它将包含搜索符号并为您处理此偏移量.
我的NSArray类别的实现在这里:http://gist.github.com/375409
所以,给定一个objects带有name属性的对象数组,我会创建一个索引数组,如下所示:
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSArray *indexedObjects = [objects indexUsingCollation:collation withSelector:@selector(name)]; // returns an array of arrays
Run Code Online (Sandbox Code Playgroud)
重要的是要注意UILocalizedIndexedCollation已经处理了以局部方式索引和分组对象的逻辑,因此不需要在这里重新发明轮子.
我的collation decorator处理搜索图标可以在这个github gist中找到.
有关使用它的更详细的教程可以在我的博客上找到.
在这种情况下,您只需传入我的collation包装器的实例,而不是上面示例中的UILocalizedIndexedCollation.