Archive for the ‘08 – 列表视图(TableViews)’ Category.
2009年08月10日, 5:12 下午
既然JSON这么好,它怎么和UITableView结合使用呢?
首先看看我们的JSON文件吧:
{
"老张家":["大张","二张","三张"],
"老李家":["大李","二李"]
}
完成的作品是这样样子的~~(点击放大阿~~)

好,开始打代码吧。
1,首先copy JSON库到当前的Project里面。

2,建立一个数据源类。我给它起名叫MyDataSource, 看看里面都有什么吧:
@interface MyDataSource : NSObject {}
+ (id)dataSource;
@end
#import "JSON.h"
@implementation MyDataSource
+ (id)dataSource
{
NSString* JSONString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"]
encoding:NSUTF8StringEncoding error:nil];
return [JSONString JSONValue];
}
@end
里面非常简单,只有一个类方法dataSource。在其中我们读取json文件的内容到一个NSString中,并用JSON框架来解读成一个NSDictionary,返回值为id。因为虽然大多的时候最外的对象都为NSDictionary,但是出于严谨,万一是NSArray不就崩溃了。所以使用id,这样其实就有再次可以用的特性了。
3,建立一个UITableViewController, 然后作适当的设置:
#import "MyTableViewController.h"
#import "MyDataSource.h"
@implementation MyTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
if (self = [super initWithStyle:style]) {
myData = [[MyDataSource dataSource] retain];
//在这里我们初始化myData,其实就是一个id对象
//传入由MyDataSource解析出的NSDictionary
}
return self;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [myData count]; //有多少个section,也就是“几家”
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[myData valueForKey:[[myData allKeys] objectAtIndex:section]] count];
//这里我们需要告诉UITableViewController每个section里面有几个,也就是“一家里面有几口人”
}
- (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 = [[myData valueForKey:[[myData allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
//这句看上去复杂,但是其实不过是在特定section里面找到对应的array,
//然后在array中找到indexPath.row所在的内容
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[myData allKeys] objectAtIndex:section];
//这里设置对应section的名字,很简单allKey返回所有的键值为一个array,也就是“张家”,“李家”
//然后用objectAtIndex: 来找出究竟是哪一个就可以了!
}
- (void)dealloc {
[myData release]; //“我们是runtime的好市民”...release就好Alan......
[super dealloc];
}
@end
4,在主程序代理 xxxAppDelegate 里面初始化这个UITableViewController然后添加它的view到window的subview中就OK拉!
5,编译运行,没有错误就万事大吉!大吉!
阿弥陀佛,祝各位愉快~
第一期请
参看iPhone上的JSON
第二期请
参看iPhone上的JSON(二)
2009年08月1日, 4:04 下午
UITableViewController 列表在iPhone开发中起着决定性的重要作用,但是UITableViewController并不是那么简单使用的,以下就是其中的重要方法和Delegate:
//这个delegate会获取有多少个"章节"
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; //这里返回的是章节(section)的个数
//如果数据源是一个复杂array或dictionary,则可以返回
return [NSArray count]; 作为章节的个数
}
//这个delegate会获取章节内有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0; //这个部分是必须要改的,否则0行的话,就是一个空表了
//如果数据源是一个复杂array或dictionary, 可以使用嵌套的查询来返回这个个数了
//比如:
return [[NSArray objectAtIndex:section] //section为整数,所以如果使用NSDictionary,
//记得使用辅助方法来计算其索引的key,或嵌套查询
count]; //因为返回的依旧是一个array或dictionary对象,所以我们获取它的大小count
}
// 这里编辑每个栏格的外观
- (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了,比如设置文字等:
cell.textField.text = [NSArray objectAtIndex:indexPath.row];
//假设这个NSArray就是本section的数据了,否则需要嵌套来查询section的信息,如前一个delegate
return cell;
}
//当栏格被点击后需要触发的事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AnotherViewController *anotherViewController = [[AnotherViewController alloc]
initWithNibName:@"AnotherView" bundle:nil];
// 建立并使用本地导航管理推入视图控制器是最普遍的方法
[self.navigationController pushViewController:anotherViewController];
[anotherViewController release];
}
// 该delegate是可选的,对那些可以被编辑的对象返回YES
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
// 对特定编辑风格进行操作
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
//以下是apple的缺省实例
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 如果是要删除那行栏格,那么就去删除数据源中的对象
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// 如果是要添加栏格,那么就应该将要添加的内容添加到数据源中
}
}
// 可选delegate,对那些被移动栏格作特定操作
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
}
// 对那些可以移动的行返回YES
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// 如果不像让栏格移动,就返回NO
return YES;
}
以上就是所有重要的delegate方法,只要数量使用这些方法,我们就能创建和自定义自己想要的iPhone表格