对象排序

时间:2013-07-25 21:11:02

标签: ios uitableview nsdictionary

我有一个包含多个部分的tableview,按我的对象的属性(日期)分组我尝试根据date的值对tableview进行排序。我为此创建了一个函数,但是我收到一个错误:

- (void)sortObjectsDictionnary:(NSArray *)arrayObjects
{
//this is my nsdictionnary
    [objects removeAllObjects]
//this is nsmutableaaray that contains dats
    [objectsIndex removeAllObjects];

    NSMutableSet *keys = [[NSMutableSet alloc] init];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/MM/dd"];

    for(int i=0;i<[arrayObjects count];i++){
        Task *myTask=[arrayObjects objectAtIndex:i];

//curentsection contains my objects whith dates
            NSMutableArray *currentSection = [objects objectForKey:taskDate];

            if (currentSection == nil)
            {
                [keys addObject:taskDate];

                currentSection = [[[NSMutableArray alloc] init] autorelease];

                [objects setObject:currentSection forKey:taskDate];


            }

            // we add objet to the right section
            [currentSection addObject:myTask];

    }
    [dateFormatter release];


    for (id element in keys) 
    { 
        [objectsIndex addObject:element];

        NSMutableArray *currentSection = [objects objectForKey:element];

        //I get an error in this line
        [currentSection sortUsingSelector:@selector(compare:)];

    }

1 个答案:

答案 0 :(得分:0)

您还没有提到错误消息是什么,我假设错误消息可能是由于访问NSMutableArray数组对象而未初始化它。之前尝试使用alloc和init for array        NSMutableArray * currentSection = [NSMutableArray] alloc] init];        currentSection = [objects objectForKey:element];

    //I get an error in this line
    [currentSection sortUsingSelector:@selector(compare:)];

嗯,这纯粹是猜测。如果不起作用,请发布您的错误消息。

巴拉斯