1
我有兩個UITableViewCell
的每個對應於Twitter Tweet
或Instagram Pic
。到目前爲止,我已經設置好了,它將在兩個單元格之間交替,並且從Twitter Tweet
變爲Instagram Pic
。不過,我想組織UITableView
,以便按照時間順序在TableView中組織Twitter Tweets
和Instagram 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]];
}
如果你們需要更多的代碼摘錄隨意問:)
非常感謝,但我將如何顯示UITableView中的Tweets/Pics? if(indexPath.row%2 == 0){ return twitterCell; } else { return instagramCell; } – Prad
@ prnk28你問如何顯示單元格中的內容? – JustAnotherCoder