`
sogotobj
  • 浏览: 615907 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

iOS5编程--官方例子代码的研究--3. TableViewSuite--1_SimpleTableView

 
阅读更多

UITableView是很重要的一个View了,这个话题有点大,因此我这里只是简单的分析这个例子给我们提供的信息。关于其他的,我会放在中级或者高级话题里面讲述。

类SimpleTableViewAppDelegate

在这个类中,我们可以学到如何动态生成一个导航模式的viewcontroller,一个table view controller,并且把table view controller作为导航模式的第一个view controller.

RootViewController *rootViewController = [[RootViewControlleralloc] initWithStyle:UITableViewStylePlain];

代码方式生成一个view controller的一个实例,注意这里没有xib文件。

需要说明的是,table view可以加载在一个普通的view controller上,也可以通过继承一个UITableViewController的方式生成,这里使用后一种方式生成。并且初始化为不分组模式(UITableViewStylePlain)。



// Retrieve the array of known time zone names, then sort the array and pass it to the root view controller.

NSArray *timeZones = [NSTimeZoneknownTimeZoneNames];

取得所有系统支持的,时区相关的地址名称,以显示在table view上。



rootViewController.timeZoneNames = [timeZonessortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

这里我们学到了一个排序的方式,通过调用NSArray的函数sortedArrayUsingSelector来排序, 这个函数的传入参数是一个函数,可以是系统提供的函数,也可以是我们自己定义的函数,这里调用定义在NSString中的函数localizedCaseInsensitiveCompare。这个函数实现字符串的大小写不敏感的比较方式。



UINavigationController *aNavigationController = [[UINavigationControlleralloc] initWithRootViewController:rootViewController];

代码的方式生成一个导航模式的view controller。把上面的view controller的实例作为根view controller。


RootViewController





self.navigationController = aNavigationController;

[aNavigationControllerrelease];

[rootViewControllerrelease];

注意上面的内存管理,我们知道,Objective-C的重点有三个,一个是内存管理,二是内存管理,三还是内存管理。

rootViewController所指向的内存在alloc的时候计数为1,在调用UINavigationController*aNavigationController = [[UINavigationControlleralloc]initWithRootViewController:rootViewController];的时候自动增1,所以这里必须release一次。aNavigationController是同样的方式,最好自己分析一下。



// Configure and display the window.

[windowaddSubview:[navigationControllerview]];

把导航模式的view加载在window上。


类RootViewController

这个类继承自UITableViewController,为了方便起见,很多时候我们也可以这么做,但是这里有一个问题,就是table view会占满整个空间,我们没法动态更改这个view的大小,如果我们使用前面的一种方式,就可以指定table view在view controller上的大小。
一个table view在显示在界面上的时候,需要调用下面这些函数,因为table view需要知道自己相关的一些数据。这些函数定义在UITableViewDataSource这个protocol里面。
一个table view必须有一个数据源(实现UITableViewDataSource的协议),还可能有一个UITableViewDelegate,这个协议定义的函数是在table view有动作的时候被调用,比如选择某一行,编辑table view等等。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

// There is only one section.

return 1;

}

一个table view分为一个或多个节(section),每一节可以有多行(row)

上面的这个函数就是返回这个table view有多少个节.

这个函数不是必须实现的。如果你不实现,那么这个节数是1。


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// Return the number of time zone names.

return [timeZoneNamescount];

}

上面这个函数返回某一节中有多少行。传入参数就是节所在index。这个函数是必须实现的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

staticNSString *MyIdentifier = @"MyIdentifier";

// Try to retrieve from the table view a now-unused cell with the given identifier.

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:MyIdentifier];

// If no cell is available, create a new one using the given identifier.

if (cell ==nil) {

// Use the default cell style.

cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:MyIdentifier] autorelease];

}

上面这几行代码是这个函数一般情况下必须写的,这里牵涉到一个内存管理,cell重用的问题,这个我会放在高级话题中讲,如果你是初级,那么先这么记着就可以了。

// Set up the cell.

NSString *timeZoneName = [timeZoneNamesobjectAtIndex:indexPath.row];

cell.textLabel.text = timeZoneName;

上面的代码更新cell上的文本。需要提到的是,如果你看到某些代码写的是cell.text = @"blabla....";这样的方式,那么这个代码是3.0以前生成的,这样的方式已经被废弃的,请不要使用。


return cell;

}


上面的这个函数是table view的数据源代理的重要函数,在显示每一节的每一行的时候都会调用这个函数。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics