我在两个单独的 NSDictionaries 中有推文和 instagram 图片,目前我只是通过将所有其他帖子设为 Tweet 来聚合帖子,而后者则是 Instagram 图片。我将如何使用这两个并组织所有 UITableViewCell 并按日期组织它们?
我在想我会从每个返回 created_at 值并将字符串转换为 NSDate,但是我将如何为每条推文和 instapic 执行此操作?
最佳答案
为了您的目的,您必须创建一个新数组,将 tweets 和 instaPics 数组合并为一个数组,然后将所有数据排序为单个数组。
合并两个数据数组如下:
NSArray *pictureArray = [tweets arrayByAddingObjectsFromArray: instaPics];
Note:: NSDictionary (NSMutableDictionary) 默认根据其键值排序。这里我们根据包含字典的特定键对数组进行排序
现在对您的 pictureArray 进行如下排序:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created_at" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
pictureArray = [pictureArray sortedArrayUsingDescriptors:sortDescriptors];
然后将你的tableview方法重新定义为::
- (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[indexPath.row];
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 = [tableView dequeueReusableCellWithIdentifier:tweetsCellIdentifier];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:instaCellIdentifier];
}
if (cell == nil) {
// Design your cell as you desired;
if (tweets) {
// Design cell for tweets
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tweetsCellIdentifier];
} else {
// Design cell for instagram
cell = [[UITableViewCell alloc] 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;
}
完成上述所有操作后,在 pictureArray 中获取数据后重新加载您的 tableview,如下所示 ..
//Reload TableView
[tableView reloadData];
它可能对你有帮助。
已编辑:: 如下所示编辑您的代码。我修改了您的 instgram 单元格创建和填充。像这样定义您的推文单元格设计和填充数据,就像为 instagram 所做的那样
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
static NSString *tweetsCellIdentifier = @"tweetsCell";
static NSString *instaCellIdentifier = @"instaCell";
UITableViewCell *cell = nil;
BOOL tweets = YES;
BOOL twitterLoggedIn = [user boolForKey:@"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[indexPath.row];
// Get cell according to your dictionary data that may be from tweets or instagram
if (tweets) {
cell = [tableView dequeueReusableCellWithIdentifier:tweetsCellIdentifier];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:instaCellIdentifier];
}
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 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tweetsCellIdentifier];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
NSDictionary *tweet = totalFeed[indexPath.row];
//Set username for twitter
NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];
UILabel *twitterNameLabel = (UILabel *)[cell viewWithTag:202];
[twitterNameLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:12.0]];
[twitterNameLabel setText:name];
//Set status for twitter
NSString *text = [tweet objectForKey:@"text"];
UILabel *twitterTweetLabel = (UILabel *)[cell viewWithTag:203];
[twitterTweetLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:10.0]];
[twitterTweetLabel setText:text];
//Set Profile Pic for twitter
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *imageUrl = [[tweet objectForKey:@"user"] objectForKey:@"profile_image_url"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *profilePic = (UIImageView *)[cell viewWithTag:201];
profilePic.image = [UIImage imageWithData:data];
//Make the Profile Pic ImageView Circular
CALayer *imageLayer = profilePic.layer;
[imageLayer setCornerRadius:25];
[imageLayer setMasksToBounds:YES];
});
});
//Set number of Favorites for Tweet
NSString *favoritesCount = [[tweet objectForKey:@"user"]objectForKey:@"favourites_count"];
UIButton *favoritesButton = (UIButton *)[cell viewWithTag:204];
[favoritesButton setTitle:[NSString stringWithFormat:@" %@",favoritesCount] forState:UIControlStateNormal];
[favoritesButton setTitle:[NSString stringWithFormat:@" %@",favoritesCount] forState:UIControlStateHighlighted];
favoritesButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:12];
} else {
// Design cell for instagram
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:instaCellIdentifier];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
UIImageView *instagramImageView = [[UIImageView alloc] init];
instagramImageView.tag = 104;
[cell.contentView addSubview:instagramImageView];
UILabel *instagramUserLabel = [[UILabel alloc] init];
instagramUserLabel.tag = 102;
[instagramUserLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:16.0]];
[cell.contentView addSubview:instagramUserLabel];
UILabel *instagramCaptionLabel = [[UILabel alloc] init];
[instagramCaptionLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:12.0]];
instagramCaptionLabel.tag = 103;
[cell.contentView addSubview:instagramCaptionLabel];
UIImageView *instagramProfilePic = [[UIImageView alloc] init];
instagramProfilePic.frame = CGRectMake(35, 31, 50, 50);
instagramProfilePic.tag = 101;
[cell.contentView addSubview:instagramProfilePic];
}
}
// 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[indexPath.row];
NSString *imageUrlString = entry[@"images"][@"low_resolution"][@"url"];
NSURL *url = [NSURL URLWithString:imageUrlString];
UIImageView *instagramImageView = (UIImageView *)[cell viewWithTag:104];
[instagramImageView setImageWithURL:url];
NSString *user = entry[@"user"][@"full_name"];
UILabel *instagramUserLabel = (UILabel *)[cell viewWithTag:102];
[instagramUserLabel setText:user];
UILabel *instagramCaptionLabel = (UILabel *)[cell viewWithTag:103];
if (entry[@"caption"] != [NSNull null] && entry[@"caption"][@"text"] != [NSNull null]) {
NSString *caption = entry[@"caption"][@"text"];
[instagramCaptionLabel setText:caption];
}else{
NSString *caption = @"";
[instagramCaptionLabel setText:caption];
}
NSString *imageUserPicUrl = entry[@"user"][@"profile_pic"][@"url"];
NSURL *profileURL = [NSURL URLWithString:imageUserPicUrl];
UIImageView *instagramProfilePic = (UIImageView *)[cell viewWithTag:101];
[instagramProfilePic setImageWithURL:profileURL];
}
return cell;
关于ios - 按创建日期组织 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319528/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下