繁体   English   中英

在TableView中按时间顺序组织单元

[英]Chronologically organize Cells in TableView

我有两个UITableViewCell ,每个都对应一个Twitter TweetInstagram Pic 到目前为止,我已经对其进行了设置,以便它将在两个单元格之间交替显示,并使其从Twitter Tweet转到Instagram Pic 但是我想组织UITableView以便它按时间顺序在TableView中组织Twitter TweetsInstagram Pics Instagram API和Twitter API的日期键均不同。 Twitter API的时间键为created_at ,Instagram的键为created_time 我将如何组织这两个数组? PS Tweets和Pics所在的阵列都是NSMutableArray的。

这是到目前为止我尝试过的代码:

    // 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]];

    }

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

看来您在正确的轨道上。 您为什么还不格式化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];
    };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM