动态数组作为节头

时间:2012-12-04 20:34:20

标签: iphone arrays tableview exc-bad-access

我有一个主数组,其中包含一大堆字典,我想要做的就是根据指定的标记对所有这些字典进行排序。这就是字典的外观:

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

1 个答案:

答案 0 :(得分: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"];
}