在uitableview上显示按日期排序的两个数组

时间:2014-01-03 21:48:16

标签: ios objective-c arrays uitableview twitter

我有两个数组,其中一个是tweets数组,包含twitter推文。另一个是instapics数组并包含Instagram图片。这两个数组都有不同的日期键。 twitter有created_at,Instagram有created_time。我想在一个UITableView上显示它们并按日期组织它们。这是我到目前为止所做的,但问题是它只显示Instagram图片:

- (void)sortArrayBasedOndate {
    NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
    [fmtDate setDateFormat:@"yyyy-MM-dd"];

    NSDateFormatter *fmtTime = [[NSDateFormatter alloc] init];
    [fmtTime setDateFormat:@"HH:mm"];

    totalFeed = [totalFeed sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        // 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];
    }];
}

上面是我试图在阵列上组织它们的方法,下面是我试图显示它们的方式:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id object = [totalFeed objectAtIndex:indexPath.row];
    if ([tweets containsObject:object]) {
        static NSString *Twitter = @"TweetCell";
        UITableViewCell *twitter = [self.tableView dequeueReusableCellWithIdentifier:Twitter];

        NSDictionary *totalArray = totalFeed[indexPath.row/2];;

// Creation Code Not shown

        return twitter;

    }else{
        static NSString *Instagram = @"InstagramCell";
        UITableViewCell *instagram = [self.tableView dequeueReusableCellWithIdentifier:Instagram];

    NSDictionary *entry = instaPics[indexPath.row / 2];
 // Creation Code not Shown
        return instagram;

    }
}

1 个答案:

答案 0 :(得分:1)

最简单的解决方案如果你添加一个共同的" creationDate"关键字中的所有对象 totalFeed数组。此密钥的值应为从NSDate创建的 " created_at"或" created_time"键。 然后你可以根据这个键对数组进行排序:

NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES]];
totalFeed = [totalFeed sortedArrayUsingDescriptors:@[sortDesc]];

如果由于某种原因你不能这样做,你必须修复比较器方法,它确实如此 根本不使用传递的参数obj1obj2。 像(伪代码):

totalFeed = [totalFeed sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSDate *date1, *date2;
    if ("obj1 is a Twitter") {
        date1 = "get created_at from twitter obj1"
    } else {
        date1 = "get created_time from instagram obj1"
    }
    if ("obj2 is a Twitter") {
        date2 = "get created_at from twitter obj2"
    } else {
        date2 = "get created_time from instagram obj2"
    }

    return [date1 compare:date2];
}];

无论如何,在cellForRowAtIndexPath您必须访问totalFeed[indexPath.row] (没有除以2,这在这里没有意义)。


更多示例代码:

NSArray *instaPics; // your instagrams
NSArray *tweets;    // your tweets
NSMutableArray *totalFeed = [NSMutableArray array]; // the common array

// Date formatter for the tweets. The date format must exactly
// match the format used in the tweets.
NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
[fmtDate setDateFormat:@"..."];

// Add all instagrams:
for (NSMutableDictionary *instagram in instaPics) {
    NSString *createdAt = instagram[@"created_time"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[createdAt doubleValue]];
    instagram[@"creationDate"] = date;
    [totalFeed addObject:instagram];
}
// Add all tweets:
for (NSMutableDictionary *twitter in tweets) {
    NSString *twitterCreated = twitter[@"created_at"];
    NSDate *date = [fmtDate dateFromString:twitterCreated];
    twitter[@"creationDate"] = date;
    [totalFeed addObject:twitter];
}

// Sort
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES];
[totalFeed sortUsingDescriptors:@[sortDesc]];