Archive for the ‘06 – 视图控制器(ViewControllers)’ Category.
2009年08月12日, 9:41 下午
错误:
UIViewController *aViewController = [[UIViewController alloc] init];
[window addSubview:aViewController.view];
[aViewController release]; |
这样的话,系统会崩溃的… window或着其他父视图是不会retain整个viewController的,这个时候不能立刻release
正确:
[aViewController.view removeFromSuperview];
[aViewController release]; |
也就是说,在release之前需要从superview中移除这个viewController的view。如果一直都需要这个view,那就在dealloc里面作这些事情就可以了
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月3日, 12:22 上午

Three20库是再好不过的iPhone开发资源了,尤其是其中提供的TT图片视图控制器提供了简单易用的图片浏览功能,下面我就简单的介绍了一下如何使用TTPhotoViewController
/*********************************************************************************************/ |
首先我们建立一个TTPhotoViewController的子类:
@interface LibDetailViewController : TTPhotoViewController { |
然后在viewDidLoad代理方法中添加数据源(datasource)
... ...
for (NSInteger i=1; i<=libLength; i++) {
picIndex = [Zeros stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
picIndex = [picIndex substringFromIndex:([picIndex length] - libDigits)];
[photosArray addObject:[[[MockPhoto alloc]
initWithURL:[NSString stringWithFormat:@"%@%@/o/%@%@%@.jpg",addr,libName,libName,addiPage,picIndex]
smallURL:[NSString stringWithFormat:@"%@%@/%@%@%@.jpg",addr,libName,libName,addiPage,picIndex]
size:CGSizeMake(320, 480)]autorelease]];
//这一行是最重要了,因为TTPhotoViewController需要的是一个TTPhotoSource作为数据源对象,
//但是这个对象其实就是一个TTPhoto的数组,所以我们在这里创建并添加所有的图片信息到一个可变数组内。
}
self.photoSource = [[[MockPhotoSource alloc]
initWithType:MockPhotoSourceDelayed
title:libTitle
photos:photosArray
photos2:nil]
autorelease];
//这里初始化并赋值这个TTPhotoSource对象到self.photoSource上
[photosArray release]; //由于self会retainphtosArray,所以可以release掉它了。 |
我在这里使用了Three20样例sample里面的类MockPhotoSource来创建TTPhoto和TTPhotoSource对象。
就这么简单,你就可以使用TTPhotoViewCotroller的便利咯~