我已经进入了索引、分段、表格 View 的兔子洞,并被带上了一段让我想破坏我房间里很多贵重元素的旅程……我希望最后一个清晰简洁的问题是有人将能够帮助我解决这个问题。
我将从头开始,以免混淆提供帮助的任何人,我认为这样更容易,希望有人可以发布一些相当不错的示例供我阅读/使用,就像我认为我之前的问题一样提供了很多细节,让每个人都感到困惑,因为请求实际上很简单。它只是制定了实现,并取决于您输入的数据以及输入的方式。
好的,我们开始吧。
我有一个 UITableView,当它打开时查询我的数据库以获取汽车制造商列表,我解析进来的 XML 并留下一个未排序的汽车制造商列表,然后我将这个 NSArray 传递给一个自定义方法,然后按 A-Z 排序。
- (IBAction)startSortingTheArray:(NSMutableArray *)arrayData
{
//Sort incoming array alphabetically
[self setSortedArray:[arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];
从这里我想知道如何为大型数据库的索引滚动制作索引 UITableview。<---->---->这是我的问题
这是我所知道的,从这一点开始,我需要创建一个字母数组,代表在我的数据中找到的字母表中的字母(即,我排序的数组中每个值的第一个字母)。我还需要创建一个 NSDictionary,将我的排序数组拆分成带有该字典的部分。
从那里我需要更新我的一些 UItableview 委托(delegate)方法,以便它们可以处理 NSDictionary 和部分等。从那里我需要将我的 NSDictionary 中的值传递到我的 UItableviewcell 标签...
在之前的所有尝试中,我已经达到了我拥有一切但实际上能够将数据显示到 tableview 单元格中的地步,但是因为它是通过阅读几个不同的教程放在一起的所以它只是不起作用。所以我想现在我实际上有点知道需要发生什么,也许有人会好心地分享他们的类似问题的代码,我将能够从中学习并希望学习如何实际做到这一点......
我要感谢迄今为止帮助过我的任何人以及 future 将要帮助我的任何人 :P 我一定会发布我的最终解决方案并附上评论,希望能防止这种情况在未来发生在其他人身上!我忍不住对 apple 对这个实现的这个泡菜感到有点仇恨。
Honda,
Honda,
Honda,
Honda,
Honda,
Honda,
Honda,
Mazda,
Mazda,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Nissan,
Nissan,
Nissan,
Nissan,
Nissan,
Nissan,
Nissan,
Toyota,
Toyota,
Toyota
最佳答案
从阅读你的问题来看,听起来你想要一个基于大型数据库的分段 UITableView。你想分几节?它可以处理任意数量的内容,但有些功能您将无法使用,例如 index - 如果您有 100 多个部分,它可能不会很好。 (事实上,我认为这可能是有限制的,但我不确定。)
在这种情况下最简单的事情就是拥有一个数组数组。该数组将为每个部分保存一个数组。所以它会是这样的:
@interface myTableView: UITableViewController {
NSMutableArray * arrayOfSection;
NSMutableArray * sectionHeaders;
}
@implementation myTableView
// Note I will assume that arrayData contains only NSStrings.
- (void)sortSectionsWithArray:(NSMutableArray *)arrayData; {
NSMutableArray * alphabet = [[NSMutableArray alloc] initWithObjects:@"A", @"B", ..., @"Y", @"Z", nil];
NSString * firstLetter;
for(NSString * string in arrayData){
firstLetter = [string substringToIndex:1];
for(NSString * sectionString in alphabet){
if([firstLetter isEqualToString:sectionString]){
[alphabet removeObject:sectionString];
break;
}
}
}
// Note that whatever is left over are the letters of the alphabet that are NOT section headers, as we removed the section headers strings.
// Therefore, if we remove the letters stored in the alphabet array, we have the remaining section headers!
if(sectionHeaders){
[sectionHeaders release];
}
sectionHeaders = [[NSMutableArray alloc] initWithObjects:@"A", @"B", ..., @"Y", @"Z", nil];
for(NSString * string in sectionHeaders){
for(NSString * sectionString in alphabet){
if([string isEqualToString:sectionString]){
[sectionHeaders removeObject:string];
break;
}
}
}
}
// Assume that the array below is your input array:
// NSArray * arrayData = [[NSArray alloc] initWithObjects:@"Honda", @"Honda", @"Mazda", @"Mazda", @"Mitsubishi", @"Mitsubishi", @"Nissan", @"Nissan", @"Toyota", @"Toyota", nil];
- (void)startSortingTheArray:(NSMutableArray *)arrayData; {
if(arrayOfSection){
[arrayOfSection release];
}
arrayOfSection = [[NSMutableArray alloc] initWithCapacity:[sectionHeaders count]];
NSMutableArray * section;
for(NSString * string in sectionHeaders){
section = [[NSMutableArray alloc] init];
for(NSString * dataString in arrayData){
if([string isEqualToString:[dataString substringToIndex:1]]){
[section addObject:dataString];
}
}
[arrayOfSection addObject:section];
[section release];
}
}
然后,在您的 UITableViewDelegate 方法中,使用:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
return [arrayOfSection count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
return [[arrayOfSection objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
static NSString * CellIdentifier = @"TableViewCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
}
// Set model saved in arrayOfSection by using: [[arrayOfSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; {
// What you want the view controller to do when a row is selected.
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; {
return [sectionHeaders objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; {
return sectionHeaders;
}
- (void)dealloc; {
[arrayOfSection release];
[sectionHeaders release];
[super dealloc];
}
希望对您有所帮助!
关于iphone - 如何为大型数据集创建 UITableView 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7479998/
出于纯粹的兴趣,我很好奇如何按顺序创建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等等),但我确实想创建一个输出文件。
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
使用带有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.现在
我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs
如何使用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方法创建的字符串从不重复?
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳