ios - 按创建日期组织 UITableViewCell
<p><p>我在两个单独的 <code>NSDictionaries</code> 中有推文和 instagram 图片,目前我只是通过将所有其他帖子设为推文来聚合帖子,而后者则是 Instagram 图片。我将如何使用这两个并组织所有 <code>UITableViewCell</code> 并按日期组织它们? </p>
<p>我在想我会从每个返回 <code>created_at</code> 值并将字符串转换为 <code>NSDate</code> 的,但我将如何为每条推文和 instapic 执行此操作?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>出于您的目的,您必须创建一个新数组,将推文和 instaPics 数组合并为一个数组,然后将所有数据排序为单个数组。 </p>
<p>合并两个数据数组如下::</p>
<pre><code>NSArray *pictureArray = ;
</code></pre>
<p><strong>注意::</strong> <code>NSDictionary (NSMutableDictionary)</code> 默认按照其键值排序。这里我们根据包含字典的特定键对数组进行排序</p>
<p>现在将您的 <strong>pictureArray</strong> 排序如下::</p>
<pre><code>NSSortDescriptor *sortDescriptor = [ initWithKey:@"created_at" ascending:YES];
NSArray *sortDescriptors = ;
pictureArray = ;
</code></pre>
<p>然后将您的 tableview 方法重新定义为::</p>
<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *tweetsCellIdentifier = @"tweetsCell";
static NSString *instaCellIdentifier = @"instaCell";
UITableViewCell *cell = nil;
BOOL tweets = NO;
// Now you get dictionary that may be of tweets array or instagram array
// Due to its different structure
// I thinks your both dictionaries have different structure
NSDictionary *pictDictionary = pictureArray;
if (your_check_condition for tweets dictionary) {
tweets = YES;
}
// Get cell according to your dictionary data that may be from tweets or instagram
if (tweets) {
cell = ;
} else {
cell = ;
}
if (cell == nil) {
// Design your cell as you desired;
if (tweets) {
// Design cell for tweets
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tweetsCellIdentifier];
} else {
// Design cell for instagram
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:instaCellIdentifier];
}
}
// Write your login to get dictionary picture data
// Tweets and Instagram array are merged. So get appropriate data with your logic
// May be both dictionaries structure are different. so write your logic to get picture data
// Fill data according tweets dict or instgram
// Get cell elements in which you will show dict data i.e. images, title etc.
if (tweets) {
// Fill cell data for tweets
} else {
// Fill cell data for instagram
}
return cell;
}
</code></pre>
<p>在得到图片数组中的数据后重新加载你的tableview如下..</p>
<pre><code>//Reload TableView
;
</code></pre>
<p>也许对你有帮助。</p>
<p><strong>已编辑::</strong>
如下编辑您的代码。我修改了您的 instgram 单元格创建和填充。像这样定义您的推文单元格设计和为 instagram 完成的填充数据</p>
<pre><code>NSUserDefaults *user = ;
static NSString *tweetsCellIdentifier = @"tweetsCell";
static NSString *instaCellIdentifier = @"instaCell";
UITableViewCell *cell = nil;
BOOL tweets = YES;
BOOL twitterLoggedIn = ;
// Now you get dictionary that may be of tweets array or instagram array
// Due to its different structure
// I thinks your both dictionaries have different structure
NSDictionary *totalFeedDictionary = totalFeed;
// Get cell according to your dictionary data that may be from tweets or instagram
if (tweets) {
cell = ;
} else {
cell = ;
}
if (cell == nil) {
// Design your cell as you desired;
if (tweets) {
// Now correct it as instagram cell design below your tweets cell design
// Only create here desired elements and its define design pattern here
// Don't Fill here At last already we fill it
// Due to scrolling cells are reuse and must be fill all time when they come from deque cel
// Design cell for tweets
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tweetsCellIdentifier];
cell.backgroundColor = ];
NSDictionary *tweet = totalFeed;
//Set username for twitter
NSString *name = [ objectForKey:@"name"];
UILabel *twitterNameLabel = (UILabel *);
];
;
//Set status for twitter
NSString *text = ;
UILabel *twitterTweetLabel = (UILabel *);
];
;
//Set Profile Pic for twitter
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *imageUrl = [ objectForKey:@"profile_image_url"];
NSData *data = ];
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *profilePic = (UIImageView *);
profilePic.image = ;
//Make the Profile Pic ImageView Circular
CALayer *imageLayer = profilePic.layer;
;
;
});
});
//Set number of Favorites for Tweet
NSString *favoritesCount = [objectForKey:@"favourites_count"];
UIButton *favoritesButton = (UIButton *);
forState:UIControlStateNormal];
forState:UIControlStateHighlighted];
favoritesButton.titleLabel.font = ;
} else {
// Design cell for instagram
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:instaCellIdentifier];
cell.backgroundColor = ];
UIImageView *instagramImageView = [ init];
instagramImageView.tag = 104;
;
UILabel *instagramUserLabel = [ init];
instagramUserLabel.tag = 102;
];
;
UILabel *instagramCaptionLabel = [ init];
];
instagramCaptionLabel.tag = 103;
;
UIImageView *instagramProfilePic = [ init];
instagramProfilePic.frame = CGRectMake(35, 31, 50, 50);
instagramProfilePic.tag = 101;
;
}
}
// Fill Data here
if (tweets) {
// Now correct as below your tweets cell filling
// Only get here desired elements and fill them here
} else {
NSDictionary *entry = totalFeed;
NSString *imageUrlString = entry[@"images"][@"low_resolution"][@"url"];
NSURL *url = ;
UIImageView *instagramImageView = (UIImageView *);
;
NSString *user =entry[@"user"][@"full_name"];
UILabel *instagramUserLabel = (UILabel *);
;
UILabel *instagramCaptionLabel = (UILabel *);
if (entry[@"caption"] != && entry[@"caption"][@"text"] != ) {
NSString *caption = entry[@"caption"][@"text"];
;
}else{
NSString *caption = @"";
;
}
NSString *imageUserPicUrl = entry[@"user"][@"profile_pic"][@"url"];
NSURL *profileURL = ;
UIImageView *instagramProfilePic = (UIImageView *);
;
}
return cell;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 按创建日期组织 UITableViewCell,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20319528/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20319528/
</a>
</p>
页:
[1]