按时间顺序组织TableView中的单元格

时间:2013-12-18 04:27:28

标签: ios uitableview sorting twitter nsarray

我有两个UITableViewCell,每个Twitter Tweet对应一个Instagram PicTwitter Tweet。截至目前,我已将其设置为在两个单元格之间交替,并使其从Instagram Pic变为UITableView。但是,我想组织Twitter Tweets,以便它在TableView中按时间顺序组织Instagram Picscreated_at。 Instagram API和Twitter API的日期键都不同。 Twitter API的时间密钥为created_time,Instagram的密钥为NSMutableArray。我如何组织这些阵列?附: Tweets和Pics所在的阵列都是 // Setup for Cells - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *twitterCell = [self createTwitterCell:indexPath]; UITableViewCell *instagramCell = [self createInstagramCell:indexPath]; if (indexPath.row % 2 == 0) { return twitterCell; }else{ return instagramCell; } } // Array Updater - (void)updateArrays { instaPics = self.timelineResponse[@"data"]; totalFeed = [tweets arrayByAddingObjectsFromArray:instaPics]; [self sortArrayBasedOndate:totalFeed]; } // Current flawed method to organize by date - (void)sortArrayBasedOndate { NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init]; [fmtDate setDateFormat:@"yyyy-MM-dd"]; NSDateFormatter *fmtTime = [[NSDateFormatter alloc] init]; [fmtTime setDateFormat:@"HH:mm"]; NSComparator compareDates = ^(id string1, id string2) { // Instagram Date Retrieval NSDictionary *instagram = self.instaPics; NSString *createdAt = instagram[@"created_time"]; int createdAtN = [createdAt intValue]; NSTimeInterval timestamp = (NSTimeInterval)createdAtN; NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:timestamp]; // Twitter Date Retrieval NSDictionary *twitter = self.tweets; NSDate *date2 = twitter[@"created_at"]; return [date1 compare:date2]; }; NSSortDescriptor * sortDesc1 = [[NSSortDescriptor alloc] initWithKey:@"start_date" ascending:YES comparator:compareDates]; [totalFeed sortUsingDescriptors:@[sortDesc1]]; } ' s。

以下是我迄今为止尝试过的代码:

{{1}}

如果你们需要更多的代码摘录,请随时询问:)

1 个答案:

答案 0 :(得分:0)

好像你走在了正确的轨道上。你为什么不格式化Twitter的时间来匹配Instagram时间的格式?

NSComparator compareDates = ^(id string1, id string2)
    {
        // Instagram Date Retrieval
        NSDictionary *instagram = self.instaPics;
        NSString *createdAt = instagram[@"created_time"];
        int createdAtN = [createdAt intValue];
        NSTimeInterval timestamp = (NSTimeInterval)createdAtN;
        NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:timestamp];

        // Twitter Date Retrieval
        NSDictionary *twitter = self.tweets;
        NSString *twitterCreated = twitter[@"created_at"];
        int createdAtTwitter = [twitterCreated intValue];
        NSTimeInterval timestampTwitter = (NSTimeInterval)createdAtTwitter;
        NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:timestampTwitter];

        return [date1 compare:date2];
    };