0

我有一个主数组,其中包含一大堆字典,我想做的是让所有这些字典根据它们分配的标签进行排序。这是字典的外观:

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;
}
4

1 回答 1

1

我认为如果您在为表视图数据源创建数组之前删除重复的“标签”,您的任务会变得更容易:

// All tags:
NSArray *tags = [mainArray valueForKey:@"tag"];
// Remove duplicates and sort:
tags = [[[NSSet setWithArray:tags] allObjects] sortedArrayUsingSelector:@selector(compare:)];

// Build an "array of arrays (of dictionaries)" as data source:
sectionsArray = [NSMutableArray array];
for (NSString *tag in tags) {
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"tag == %@", tag];
    NSArray *onesection = [mainArray filteredArrayUsingPredicate:pred];
    [sectionsArray addObject:onesection];
}

例如,如果mainArray

(
    { date = "2012-12-04 20:26:04 +0000"; name = H; tag = "#J"; },
    { date = "2013-12-04 20:26:04 +0000"; name = X; tag = "#J"; },
    { date = "2014-12-04 20:26:04 +0000"; name = Z; tag = "#L"; }
)

那么sectionsArray将是

(
    (
        { date = "2012-12-04 20:26:04 +0000"; name = H; tag = "#J"; },
        { date = "2013-12-04 20:26:04 +0000"; name = X; tag = "#J"; }
    ),
    (
        { date = "2014-12-04 20:26:04 +0000"; name = Z; tag = "#L"; }
    )
)

您可以轻松访问每个部分和部分中的每一行:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [sectionsArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[sectionsArray objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = ...;
    }
    NSDictionary *dict = [[sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.text = [dict objectForKey:@"name"];
    cell.detailTextLabel.text = [dict objectForKey:@"date"];
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *dict = [[sectionsArray objectAtIndex:section] objectAtIndex:0];
    return [dict objectForKey:@"tag"];
}
于 2012-12-04T21:58:36.970 回答