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的数据源代理的重要函数,在显示每一节的每一行的时候都会调用这个函数。
分享到:
相关推荐
Microsoft_Third-Party-Notices_Microsoft-Edge-for-iOS_2021-08-03.docx
ios-cmake-master用来交叉编译的,很好用
# CentOS7.6 下载地址 # CentOS-7-x86_64-DVD-1810.iso CentOS 7.6 DVD 版 4G ... # CentOS-7-x86_64-Everything-1810.iso CentOS 7.6 Everything版 10G ...
SLE-12-SP5-Server-DVD-x86_64-GM-DVD1.iso
iOS_iPadOS_16_Beta_Profile.mobileconfig
资源来自pypi官网。 资源全名:py_ios_device-2.1.5.tar.gz
一步一步学习iOS5编程-项目源代码
Cisco 思科 1921 路由器 IOS c1900-universalk9-mz.SPA.158-3.M4.rar 官网下载
【逗老师带你学IT】vMware ESXi 6.7合并第三方硬件驱动 https://blog.csdn.net/ytlzq0228/article/details/105194719
vuforia-sdk-ios-6-5-25.zip vuforia sdk for iOS Use the Vuforia SDK to build Android, iOS, and UWP(win10+) applications for mobile devices and digital eyewear. Apps can be built with Android Studio, ...
CentOS-7-x86_64-DVD-1611.iso镜像,可以在虚拟机上安装CentOS-7系统,学习linux,计算机专业学习必备
iOS 11 by tutorials 源码(第9至15章) http://download.csdn.net/user/xrick
一步一步学习 iOS 5编程-SimpleTable 项目,源代码实现到第九部分。
python库。 资源全名:jodel_ios_api-1.0.2-py2-none-any.whl
Xamarin.iOS_Setup-1.10.39
iOS---UIKit Dynamics源代码,
linux虚拟机镜像文件
网易airtest for mac必备agent, iOS-Tagent 其实就是 WebDriverAgent 仔细看,会发现iOS-Tagent会比WebDriverAgent简略得多。 iOS-Tagent其实就是WDA的优化版本。 git clone ...
IOS代码示例-登录+界面切换+界面间传参,代码简洁,亲测可用。IOS代码示例-登录+界面切换+界面间传参,代码简洁,亲测可用。IOS代码示例-登录+界面切换+界面间传参,代码简洁,亲测可用。IOS代码示例-登录+界面切换+...