- 浏览: 1504460 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (419)
- XMPP (19)
- Android (180)
- Java (59)
- Network (4)
- HTML5 (13)
- Eclipse (9)
- SCM (23)
- C/C++ (4)
- UML (4)
- Libjingle (15)
- Tools&Softwares (29)
- Linphone (5)
- Linux&UNIX (6)
- Windows (18)
- Google (10)
- MISC (3)
- SIP (6)
- SQLite (5)
- Security (4)
- Opensource (29)
- Online (2)
- 文章 (3)
- MemoryLeak (10)
- Decompile (5)
- Ruby (1)
- Image (1)
- Bat (4)
- TTS&ASR (28)
- Multimedia (1)
- iOS (20)
- Asciiflow - ASCII Flow Diagram Tool.htm (1)
- Networking (1)
- DLNA&UPnP (2)
- Chrome (2)
- CI (1)
- SmartHome (0)
- CloudComputing (1)
- NodeJS (3)
- MachineLearning (2)
最新评论
-
bzhao:
点赞123!
Windows的adb shell中使用vi不乱码方法及AdbPutty -
wahahachuang8:
我觉得这种东西自己开发太麻烦了,就别自己捣鼓了,找个第三方,方 ...
HTML5 WebSocket 技术介绍 -
obehavior:
view.setOnTouchListenerview是什么
[转]android 一直在最前面的浮动窗口效果 -
wutenghua:
[转]android 一直在最前面的浮动窗口效果 -
zee3.lin:
Sorry~~
When I build "call ...
Step by Step about How to Build libjingle 0.4
http://www.iphonedevcentral.com/indexed-uitableview-tutorial/
Tackling that handy index on the right side of table views is really not that complicated. In fact, it only requires that you implement two methods. In this tutorial, I’m going to take you step-by-step on how to wire it all together.
Prerequisites
If you were able to follow my previous tutorials , you’re going to be just fine with this one. I assume that you have iPhone SDK 3.x downloaded and installed, know how to create an empty project and files in Xcode and know a thing or two about Objective-C and Cocoa touch. While I am using iPhone SDK 3.2 Beta, I compiled this project against version 3.1.3, which is the current live version.
Let’s get started!
1 .Set up your project
In Xcode, go to File -> New Project and choose Navigation-based Application from the iPhone OS tab. Name the application IndexedTable . At this point, you should have a runnable app with an empty table in it.
2. Create a data source
Instead of hardcoding a bunch of values, country names and whatnot, let’s create a simple method that will generate a bunch of strings from the letters of the alphabet so that our UITableView has something to work with.
Create a new class called DataGenerator . Right-click on the Classes group in the project browser and choose New File . Let the file be an Objective-C class (subclass of NSObject) and name it DataGenerator . This class will only have one static method (for now) that will simply return an array of words from letters. As such, we’ll name it wordsFromLetters .
In DataGenerator.h , insert the following code:
@interface DataGenerator : NSObject { } + (NSArray *) wordsFromLetters; @end
Now, let’s implement this method. Open DataGenerator.m and put this code in it:
@implementation DataGenerator #define WORD_LENGTH 5 static NSString *letters = @"abcdefghijklmnopqrstuvwxyz"; + (NSArray *) wordsFromLetters { NSMutableArray *content = [NSMutableArray new]; for (int i = 0; i < [letters length]; i++ ) { NSMutableDictionary *row = [[[NSMutableDictionary alloc] init] autorelease]; char currentWord[WORD_LENGTH + 1]; NSMutableArray *words = [[[NSMutableArray alloc] init] autorelease]; for (int j = 0; j < WORD_LENGTH; j++ ) { if (j == 0) { currentWord[j] = toupper([letters characterAtIndex:i]); } else { currentWord[j] = [letters characterAtIndex:i]; } currentWord[j+1] = '\0'; [words addObject:[NSString stringWithCString:currentWord encoding:NSASCIIStringEncoding]]; } char currentLetter[2] = { toupper([letters characterAtIndex:i]), '\0'}; [row setValue:[NSString stringWithCString:currentLetter encoding:NSASCIIStringEncoding] forKey:@"headerTitle"]; [row setValue:words forKey:@"rowValues"]; [content addObject:row]; } return content; } @end
I’m not going to spend a whole of time explaining what’s going on here because this tutorial is really about adding an index bar to your UITableView. But let’s briefly talk what’s it all about. This method loops through an array letters one-by-one. For each letter it then generates a bunch of words to fill up our content. The final structure of the NSArray we’re returning is going to look something like this:
NSArray =>
-
NSDictionary
- headerTitle => ‘A’
- rowValues => {”A”, “Aa”, “Aaa”, “Aaaa”}
-
NSDictionary
- headerTitle => ‘B’
- rowValues => {”B”, “Bb”, “Bbb”, “Bbbb”}
- etc.
You’ll see how we’re using this array later on. Also note that this list is implicitly ordered.
3. Fill in UITableView with values from DataGenerator
Next we’re going to populate the currently empty table with data we get from our data generator. Open RootViewController.h and add these two instance variables to the class:
#import "DataGenerator.h" @interface RootViewController : UITableViewController { NSArray *content; NSArray *indices; }
content will hold our array of dictionaries while indices will hold all initial letters for each word in the list. Let's fill those up. Open RootViewController.m implementation file and override the method viewDidLoad with the following:
- (void)viewDidLoad { [super viewDidLoad]; content = [DataGenerator wordsFromLetters]; indices = [[content valueForKey:@"headerTitle"] retain]; }
You can see that we're using [DataGenerator wordsFromLetters] to simply fill in the content variable. The second line of that method returns all keys from the dictionaries in content as an array. So indices now holds all letters of the alphabet.
We next override the two methods that tell our UITableView how many sections it has and how many rows there are in each section.
// Customize the number of sections in the table view. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [content count]; } // Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[[content objectAtIndex:section] objectForKey:@"rowValues"] count] ; }
The number of sections is equal to the number of letters in our list and the number of rows of each section is equal to the count of each array under its corresponding letter.
Finally, we implement cellForRowAtIndexPath so that it displays words from our list in the table. We handle headers for sections in titleForHeaderInSection . This method queries our content at a particular section and demands a headerTitle to be returned as header of that section.
// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = [[[content objectAtIndex:indexPath.section] objectForKey:@"rowValues"] objectAtIndex:indexPath.row]; return cell; }
You should be able to run your app now (CMD + R) and see something like this:
4. Add index to the table
There is nothing new in what we've done so far. We simply filled in a UITableView with some data. We're now ready to add our index to it. For that, we'll need to implement two methods: sectionIndexTitlesForTableView and sectionForSectionIndexTitle .
Add this code to RootViewController.m :
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [content valueForKey:@"headerTitle"]; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return [indices indexOfObject:title]; }
sectionIndexTitlesForTableView: In order to properly render the index, the UITableView needs to know all index titles to display. This method returns an array of strings containing all indices. In our case, A, B, C, D, etc.
sectionForSectionIndexTitle: When you start scrolling through the index, the table needs to know how far down/up to scroll so that the letter you're touching corresponds the appropriate section in the table. This method returns an index value (integer) of the section you're currently touching in the index.
One more thing we need to do before we run our app is to add titles to each section header in UITableView.
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { return [[content objectAtIndex:section] objectForKey:@"headerTitle"]; }
5. Run your app
If everything went well, you should now be able to run your app and see the final product. You'll see the index on the right and scrolling over it will scroll the table appropriately.
Conclusion
Voilà! That wasn't that hard was it? The main get-away from this tutorial are the two methods you need to implement in order for the index to show up and function properly: sectionIndexTitlesForTableView : and sectionForSectionIndexTitle :. In the next tutorial, we'll cover how to add a search bar to this table.
The complete source code for this tutorial can be found here: Indexed UITableView
- IndexedTable.zip (22 KB)
- 下载次数: 5
发表评论
-
Nuance - Dragon Mobile SDK - Speech Kit
2012-07-02 15:57 1415http://dragonmobile.nuancemobil ... -
iOS的开源库和开源项目
2012-06-14 10:54 1025http://www.cocoachina.com/iphon ... -
[iOS开发教程-4]Create a UITabBarController from scratch
2012-06-13 15:20 1434http://www.iphonedevcentral.com ... -
[iOS开发教程-3]Create a Detail View Page using UIImageView, UITextView and UILabel
2012-06-13 14:11 2136http://www.iphonedevcentral.com ... -
[iOS开发教程-2]Customize that UIViewCell – Part 1: Using Interface Builder
2012-06-13 13:00 1488http://www.iphonedevcentral.com ... -
[iOS开发教程-1]Hello UITableView!
2012-06-13 11:12 2083http://www.iphonedevcentral.com ... -
Window 7主机与VMware中Mac虚拟机共享文件夹
2012-06-08 23:28 191421. 确保针对Mac虚拟机的VMware Tools的安装 ... -
VMware 8.02虚拟机安装MAC lion 10.7.3教程 附送原版提取镜像InstallESD.iso!
2012-06-08 23:14 6365http://www.winthink.net/thread- ... -
[iOS]深入浅出 iOS 之多线程 NSThread
2012-06-08 15:30 21310http://www.cocoachina.com/bbs/r ... -
Object-C中的Selector概念
2012-06-08 15:25 1103selector可以叫做选择器,其实指的就是对象的方法,也 ... -
CodeBlocks集成Objective-C
2012-06-05 23:35 1194http://www.jxva.com/?act=blog!a ... -
用ultraEdit打造自己的Objective-C IDE for Windows
2012-06-05 21:41 1488http://blog.csdn.net/tomatofly/ ... -
Windows下UltraEdit查看Objective-C代码高亮工具
2012-06-05 21:26 2497http://www.cocoachina.com/bbs/r ... -
Objective-C 编程教程(全)
2012-06-04 23:32 1284http://www.youku.com/playlist_s ... -
iPhone开发学习笔记
2012-06-04 22:15 2685http://blog.csdn.net/huanglx198 ... -
Installing and using GNUstep and Objective-C on Windows
2012-06-04 16:49 1052http://www.techotopia.com/index ... -
GNUStep
2012-06-04 16:45 1246http://www.gnustep.org/ T ... -
Objective C programming in Windows – GNUStep & ProjectCenter
2012-06-04 16:12 1154http://www.jaysonjc.com/program ... -
The Objective-C Programming Language
2012-06-03 19:31 1033http://developer.apple.com/libr ...
相关推荐
5. **内存管理**:考虑到前端资源限制,RDF-Dataset-Indexed 还可能包含了内存优化策略,以确保在不影响性能的同时,不会过度消耗浏览器内存。 6. **社区支持**:作为开源项目,RDF-Dataset-Indexed 拥有活跃的社区...
ngx-neo-indexeddb ngx-neo-indexeddb是angular2-indexeddb的纯Angular> = 2工作版本 。 它公开了非常简单的Promise API,以启用IndexedDB的使用,而无需进行大部分测试。安装要安装此库,请运行: $ npm install ...
Cordova的异步IndexedDB插件 特征 使用填充不支持IndexedDB的设备 在Windows设备上使用 在iOS设备上使用 可以选择通过替换设备上的本机IndexedDB 可以选择在设备上增强本机IndexedDB 这个插件基本上是一个Indexed...
ngx-indexed-db ngx-indexed-db是一项服务,结合了ngx-indexed-db ,将IndexedDB数据库包装在Angular服务中。安装$ npm install ngx-indexed-db或者$ yarn add ngx-indexed-db用法导入NgxIndexedDBModule并启动它: ...
indexeddb-promiseIndexedDB的所有操作都是异步的,API都是在回调函数中执行的,为了便于对其进行操作,使用ES6中的Promise来封装。1. 安装npm包// use npmnpm install --save-dev indexeddb-promise// use yarnyarn...
**二叉索引树(Binary Indexed Tree,BIT)**,又称 Fenwick 树,是数据结构中的一个重要概念,尤其在前端工程师的面试中常被问到。它是一种高效的数据结构,用于快速处理数组中区间的求和问题以及部分更新操作。在...
数据集包含关于美国五年期...5-year-treasury-inflation-indexed-security-constant-maturity_metadata.json 5-year-treasury-inflation-indexed-security-constant-maturity_metadata_1.json DFII5.csv FII5.csv
Apple的Safari WebKit提供了对IndexedDB的完整支持,使得开发人员可以在iOS的Web应用或使用WKWebView的Swift或Objective-C项目中使用。然而,原生的iOS应用通常会使用Core Data或SQLite等本地数据库解决方案,因为...
但是如果在浏览器上需要用到数据库一般会使用Indexed DB数据库,webSQL基本上已经废弃了,具体原因小伙伴可以下来自己查查。 IndexedDB 是一种底层 API,用于在客户端存储大量的结构化数据(也包括文件/二进制大型...
结果我放弃了开发,直到 indexeddb 得到更广泛的实现和更一致的 ** 安装 ember install:addon ember-cli-indexeddb-wrapper ember g indexeddb-service 这将生成一个文件,您可以使用该文件来配置服务 // ...
Ember-cli-indexeddb 此自述文件概述了在此 Ember 插件上进行协作的详细信息。 安装 git clone这个仓库 npm install bower install 跑步 ember server 在访问您的应用程序。 运行测试 ember test ember test --...
从IndexedDB导出并导入另一个IndexedDB的Javascript代码,以便导出所有“ Great Suspender”保存的会话,并将它们导入到另一个扩展的IndexedDB(如果兼容)中,或在先前版本的“ The Great Suspender”中导入 ...
IndexedDB API模拟 浏览器IndexedDB API的单元测试模拟实现。 尽可能符合 (1.0版)。 重要提示: IndexedDB 1.0规范有两个突出功能,但该模拟不支持这些功能: 强制执行唯一索引的约束(在put()和add()等上抛出DOM...
web-worker-indexeddb 使用 Web Worker 访问服务器,并将数据保存在 IndexedDB 中以供离线使用。 在 IndexedDB 中保存数据后,应用程序将始终离线访问数据,并将通过 Web Worker 刷新数据。
在iOS开发中,UITableView是用于显示数据的一种常见控件,它可以以列表的形式展示信息,而UITableViewCell则是构成这个列表的基本单元,每一行数据对应一个单元格。以下是对UITableView和UITableViewCell的详细解析...
在IT领域,尤其是在Web开发中,SQLJs和IndexedDB是两个重要的存储技术。SQLJs是一个JavaScript库,它允许在浏览器环境中使用SQLite数据库,而IndexedDB是Web浏览器内置的一个非关系型(NoSQL)数据库系统。这两者都...
使用 IndexedDB 和缓存提高性能这是 LinkedIn 学习课程使用 IndexedDB 和缓存提高性能的存储库。 完整课程可从。 随着开发人员努力创建高效工作且可以容忍不可靠网络连接的 Web 应用程序,缓存和本地存储方法变得...
vue-idb基于Dexie的Vuejs索引数据库包装npm install vue-idb --save用法从'vue'导入Vue从'vue-idb'导入VueVue.us vue-idb基于Dexie安装的Vuejs索引数据库包装npm install vue-idb-保存用法从'vue'导入Vue从'vue-idb'...
IndexedDB简介:浏览器内数据库 应用程序需要数据。 对于大多数Web应用程序,数据存储在服务器上进行组织和管理,并通过网络请求将其提供给客户端。 随着浏览器的功能越来越强大,用于存储和操作应用程序数据的选项...