NSPredicate 帮助计算数组中的对象
我有一个包含字典的数组,我想根据每个字典中的“名称”字段在表视图中显示值。假设名称以“a”开头;那么“b”和“c”位于不同的部分。部分的数量对应于词典的“名称”字段。如何获取行数?如果有人知道请给出解决方案。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//---get the letter in each section; e.g., A, B, C, etc.---
NSString *alphabet = [subscribeIndexArray objectAtIndex:section];
//---get all states beginning with the letter---
//NSMutableArray *states =[[NSMutableArray alloc]init];
NSArray *states;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
for(NSDictionary *dict in subscribeViewArray1)
{
states =[[dict valueForKey:@"name"] filteredArrayUsingPredicate:predicate];
}
//---return the number of states beginning with the letter---
return [states count];
}
I have an array containing dictionaries, and I want to show the values in a table view according to the "name" field in each dictionary. Suppose the name starts with "a"; then "b" and "c" are in different sections. The number of sections corresponds to the "name" field of the dictionaries. How do I get the number of rows? If any one knows, please give the solution.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//---get the letter in each section; e.g., A, B, C, etc.---
NSString *alphabet = [subscribeIndexArray objectAtIndex:section];
//---get all states beginning with the letter---
//NSMutableArray *states =[[NSMutableArray alloc]init];
NSArray *states;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
for(NSDictionary *dict in subscribeViewArray1)
{
states =[[dict valueForKey:@"name"] filteredArrayUsingPredicate:predicate];
}
//---return the number of states beginning with the letter---
return [states count];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将状态名称放入数组中,以便使用谓词过滤它们。
试试这个:
You need to have the state names in an array in order to filter them with a predicate.
Try this: