2013-12-18 29 views
1

我有兩個UITableViewCell的每個對應於Twitter TweetInstagram Pic。到目前爲止,我已經設置好了,它將在兩個單元格之間交替,並且從Twitter Tweet變爲Instagram Pic。不過,我想組織UITableView,以便按照時間順序在TableView中組織Twitter TweetsInstagram Pics。 Instagram API和Twitter API的日期鍵都不同。 Twitter API的時間密鑰爲created_at,Instagram的密鑰爲created_time。我將如何組織這兩個陣列?附: Tweets和Pics所在的陣列都是NSMutableArray's。按時間順序組織TableView中的單元格

這裏是我迄今爲止嘗試過的代碼:

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

    } 

如果你們需要更多的代碼摘錄隨意問:)

回答

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

非常感謝,但我將如何顯示UITableView中的Tweets/Pics? if(indexPath.row%2 == 0){ return twitterCell; } else { return instagramCell; } – Prad

+0

@ prnk28你問如何顯示單元格中的內容? – JustAnotherCoder

相關問題