【问题标题】:Dynamic arrays as section headers动态数组作为节标题
【发布时间】:2012-11-22 13:55:43
【问题描述】:

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

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

【问题讨论】:

    标签: iphone arrays tableview exc-bad-access


    【解决方案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"];
    }
    

    【讨论】:

    • 它在NSDictionary *dict = [[sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 处抛出异常EXC_BAD_ACESS
    • @Ravin455:我在发布之前在一个测试项目中检查了我的答案,它对我有用。 - 或许你可以检查cellForRowAtIndexPathsectionsArrayindexPath的值来定位问题。
    • 我添加了一个boolean 所以像这样if (isTagFilterOn==YES) { // the code } 然后做[self.tableview reloadData]
    • 谢谢!我会尝试自己破解它,如果找到解决方案,我会发布。
    猜你喜欢
    • 2019-11-21
    • 1970-01-01
    • 2021-11-18
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多