我有一个主数组,其中包含一大堆字典,我想做的是让所有这些字典根据它们分配的标签进行排序。这是字典的外观:
date = "2012-12-04 20:26:04 +0000";
name = H;
tag = "#J";
下面是主数组的外观:
MAIN_ARRAY
- dict1
- dict2
- dict3
我想像这样对主数组进行排序:
MAIN_ARRAY
- tag1
- dict1
- dict2
- tag2
- dict3
这是我的代码:
-(NSArray *)returnTagContent {
NSArray *tags = [all valueForKey:@"tag"];
NSMutableArray *adoptTags = [[[NSMutableArray alloc] init] autorelease];
for (NSString *tagQuery in tags) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"tag CONTAINS[cd] %@", tagQuery];
NSArray *roughArray = [all filteredArrayUsingPredicate:predicate];
NSArray *tagContent = [[NSSet setWithArray:roughArray] allObjects];
[adoptTags addObject:tagContent];
}
return adoptTags;
}
它返回数组,但现在我想将它组织成节标题。我该怎么办?
我还有另一段代码返回部分标题标题有问题:
-(NSString *)returnTitleForTags {
NSString *uniqueTag = nil;
for (NSArray *tagContent in allTags) {
uniqueTag = [[[tagContent valueForKey:@"tag"] allObjects] lastObject];
}
return uniqueTag;
}
问题?好吧,我知道这是因为检索数组对象的lastObject
任何其他想法。NSString
更新:新代码更改。
我更新数组以在单击按钮时显示部分,如下所示:
isTagFilterOn=YES;
[self loadSectionsArray];
[self.tableView reloadData];
这是代码cellForRowAtIndexPath:
if (isTagFilterOn==YES) {
NSDictionary *dict = [[sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = [dict valueForKey:@"name"];
cell.detailTextLabel.text = [dict valueForKey:@"date"];
}
else {
NSString *object = all[indexPath.row];
cell.textLabel.text = [object valueForKey:@"name"];
cell.detailTextLabel.text = [object valueForKey:@"tag"];
}
其余的部分
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (isTagFilterOn==YES) {
return [sectionsArray count];
}
else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isTagFilterOn==YES) {
return [[sectionsArray objectAtIndex:section] count];
}
return all.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (isTagFilterOn==YES) {
NSDictionary *dict = [[sectionsArray objectAtIndex:section] objectAtIndex:0];
return [dict objectForKey:@"tag"];
}
return nil;
}