在iOS中基于NSDate向NSMutableArray添加项目

时间:2014-05-09 03:47:10

标签: ios uitableview nsmutablearray nsdate

在我的iOS本机应用程序中,我解析XML文件,使用名为RSSEntry的类解析所有内容。它从pubDate获取日期并将其存储为articleDate,即NSDate。解析完所有内容后,运行此代码:

- (void)requestFinished:(ASIHTTPRequest *)request {

    [_queue addOperationWithBlock:^{

        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
                                                               options:0 error:&error];
        if (doc == nil) {
            NSLog(@"Failed to parse %@", request.url);
        } else {

            NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntry *entry in entries) {

                    int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                        RSSEntry *entry1 = (RSSEntry *) a;
                        RSSEntry *entry2 = (RSSEntry *) b;
                        return [entry1.articleDate compare:entry2.articleDate];
                    }];

                    [_allEntries insertObject:entry atIndex:insertIdx];
                    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:UITableViewRowAnimationRight];

                }

            }];

        }
    }];
    [self.refreshControl endRefreshing];

}

这会将每个条目添加到UITableView中,按日期排序。但是,某些日期将在未来。我想只添加当天或更早版本的pubDate,消除未来的条目,直到它们的日期接近为止。建议?

2 个答案:

答案 0 :(得分:2)

在班级中添加此方法

- (BOOL)isDate:(NSDate *)startDate earlierThanDate:(NSDate *)endDate
{
    if ([startDate compare:endDate] == NSOrderedAscending) {
        return YES;
    } else if ([startDate compare:endDate] == NSOrderedSame) {
        return YES;
    } else {
        return NO;
    }

}

然后在将对象添加到数组之前,使用此方法检查它是否是将来的日期

- (void)requestFinished:(ASIHTTPRequest *)request {

    [_queue addOperationWithBlock:^{

        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
                                                               options:0 error:&error];
        if (doc == nil) {
            NSLog(@"Failed to parse %@", request.url);
        } else {

            NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntry *entry in entries) {
                    if ([self isDate:[NSDate date] earlierThanDate:entry.articleDate])  {

                        int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                           RSSEntry *entry1 = (RSSEntry *) a;
                           RSSEntry *entry2 = (RSSEntry *) b;
                           return [entry1.articleDate compare:entry2.articleDate];
                        }];

                          [_allEntries insertObject:entry atIndex:insertIdx];
                          [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:UITableViewRowAnimationRight];

                    }



                }

            }];

        }
    }];
    [self.refreshControl endRefreshing];

}

答案 1 :(得分:0)

想到几个想法:

  • 解析xml时,如果日期是将来,请不要将对象添加到最终数组中。

  • 我假设您的numberOfRowsInSection方法返回数组中的元素数。如果是这种情况,则找到其日期为将来的第一个项目的索引,然后返回小于该值的1。这应该可以工作,因为您的数组已排序。这样做的好处是可以轻松地重新加载tableview,并且之前没有显示剪切的行没有额外的工作。