【问题标题】:display two arrays on uitableview sorted by date在 uitableview 上显示两个按日期排序的数组
【发布时间】:2014-01-21 16:35:46
【问题描述】:

我有两个数组,其中一个是 tweets 数组并包含 twitter 推文。另一个是 instapics 数组,包含 Instagram 图片。这两个数组都有不同的日期键。推特有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;

    }
}

【问题讨论】:

  • 您的排序过程没有使用传递给它的对象;它没有正确排序数组。我不能确定,但​​看起来它是使用两个提要的完整创建日期进行排序的。
  • @JoshCaswell 对不起,我是 iOS 新手,你能告诉我该怎么做吗?
  • 对您不了解的内容更具体。

标签: ios objective-c arrays uitableview twitter


【解决方案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]];

【讨论】:

  • 您能告诉我如何创建通用的创建日期键吗?
  • @prnk28: twitter/instagram 对象是 dictionaries 吗?
  • 是的,这些对象确实是字典
  • @prnk28:更好。而 created_at/created_time 是存储为 NSString 还是 NSDate?如果存储为字符串,如何格式化?
  • 它被创建为 NSString,我手动将其格式化为 NSDates。
猜你喜欢
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
相关资源
最近更新 更多