简体   繁体   English

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

[英]display two arrays on uitableview sorted by date

I have two arrays one of which is a tweets array and contains twitter tweets. 我有两个数组,其中之一是tweets数组,其中包含twitter tweets。 Another of which is a instapics array and contains Instagram pictures. 另一个是instapics数组,其中包含Instagram图片。 Both of these Arrays have different keys for date. 这两个数组都具有不同的日期键。 The twitter one had created_at and the instagram one has created_time . Twitter的人有created_at和Instagram的一个具有created_time I want to display both of them on a single UITableView and have them organized by date. 我想将它们都显示在一个UITableView并按日期组织它们。 Here is what I am doing so far, the problem with this however is that it only shows Instagram Pictures: 到目前为止,这是我正在做的事情,但是问题是它只显示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];
    }];
}

The above is how I am trying to organize them on the array, below is how I am attempting to display them: 上面是我试图在数组上组织它们的方法,下面是我试图显示它们的方法:

- (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;

    }
}

The easiest solution would be if you add a common "creationDate" key to all objects in the totalFeed array. 最简单的解决方案是,如果向totalFeed数组中的所有对象添加通用的“ creationDate”键。 The value of this key should be an NSDate created from the "created_at" or "created_time" key. 该键的值应该是从“ created_at”或“ created_time”键创建的NSDate Then you can just sort the array according to this key: 然后,您可以根据此键对数组进行排序:

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

If you cannot do that for some reason, you have to fix the comparator method, it does not use the passed arguments obj1 , obj2 at all. 如果由于某种原因不能执行此操作,则必须修复比较器方法,该方法完全不使用传递的参数obj1obj2 Something like (pseudo-code): 类似于(伪代码)的东西:

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

In any case, in cellForRowAtIndexPath you have to access totalFeed[indexPath.row] (without dividing by 2, which does not make sense here). 无论如何,在cellForRowAtIndexPath您必须访问totalFeed[indexPath.row] (不除以2,在这里没有意义)。


More sample code: 更多示例代码:

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

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

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