我有一個UITableView,包含我想按第一個字母(類似於地址簿應用程序)分組(排序)的名稱。我目前能夠搭配任何部分( 'A' - 'Z')使用:可可觸摸UITableView按字母順序排列'#'全部匹配無匹配
// 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];
然而,對於 '#' 匹配我摸不透。我試着用構建一個正則表達式匹配:
[NSPredicate predicateWithFormat:@"name matches '[^a-zA-Z].*'"];
但失敗了變音-α(重複行出現)。任何想法將不勝感激!謝謝。
感謝您的快速回復盧克!我現在只是看看現在的一切,並做了修復,看看它是否工作!乾杯。 – 2010-04-22 16:05:21