通过两种方法来实现:
一、通过动态数组NSMutableArray中的数据,来显示数据
1.新建Empty Application项目,新建ViewController,HomeViewController,在AppDelegate.m中导入该文件,并在方法- (BOOL)application:didFinishLaunchingWithOptions:中添加以下红色标记的代码。
| 1 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 2 | { |
| 3 | self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
| 4 | // Override point for customization after application launch. |
| 5 | self.window.backgroundColor = [UIColor whiteColor]; |
| 6 | |
| 7 | HomeViewController *homeViewController = [[HomeViewController alloc] init]; |
| 8 | self.window.rootViewController = homeViewController; |
| 9 | [homeViewController release]; |
| 10 | |
| 11 | [self.window makeKeyAndVisible]; |
| 12 | return YES; |
| 13 | } |
2.在 HomeViewController.xib上添加Table View控件
将其Outlets的dataSource和delegate与File‘s Owner建立关联,目的:
(1) dataSource: 向HomeViewController添加UITableViewDataSource协议,从而可以在该类中使用相关的协议方法,在Table View中显示数据。
(2) delegate :向HomeViewController添加UITableViewDelegate协议,从而可以在该类中使用相关的协议方法,响应用户在Table View中的交互操作。
在HomeViewController.h中添加协议:
| 1 | #import <UIKit/UIKit.h> |
| 2 | @interface HomeViewController : UIViewController |
| 3 | <UITableViewDelegate, UITableViewDataSource>{ |
| 4 | } |
| 5 | @end |
目的:都添加协议,有备无患。提高代码编写的效率和可靠性。
3. 在HomeViewController.m中编写代码
| 1 | #import "HomeViewController.h" |
| 2 | @interface HomeViewController () |
| 3 | @end |
| 4 | @implementation HomeViewController |
| 5 | NSMutableArray *listOfContacts;//声明动态数组 |
| 6 | - (void)viewDidLoad |
| 7 | { |
| 8 | |
| 9 | listOfContacts = [[NSMutableArray alloc] init];//分配内存并初始化 |
| 10 | |
| 11 | [listOfContacts addObject:@"张三"]; |
| 12 | [listOfContacts addObject:@"张1"]; |
| 13 | [listOfContacts addObject:@"张2"]; |
| 14 | [listOfContacts addObject:@"张3"]; |
| 15 | [listOfContacts addObject:@"张4"]; |
| 16 | [listOfContacts addObject:@"张5"]; |
| 17 | [listOfContacts addObject:@"张6"]; |
| 18 | [listOfContacts addObject:@"张7"]; |
| 19 | [listOfContacts addObject:@"张8"]; |
| 20 | [listOfContacts addObject:@"张9"]; |
| 21 | [listOfContacts addObject:@"张11"]; |
| 22 | [listOfContacts addObject:@"张12"]; |
| 23 | [listOfContacts addObject:@"张13"]; |
| 24 | [listOfContacts addObject:@"张14"]; |
| 25 | [listOfContacts addObject:@"张15"]; |
| 26 | [listOfContacts addObject:@"张16"]; |
| 27 | [listOfContacts addObject:@"张17"]; |
| 28 | [listOfContacts addObject:@"张18"]; |
| 29 | [listOfContacts addObject:@"张19"]; |
| 30 | |
| 31 | [super viewDidLoad]; |
| 32 | } |
| 33 | //使用UITableViewDataSource协议的tableView:cellForRowAtIndexPath:方法 |
| 34 | //该方法用来将数据填充进Table View的单元格中 |
| 35 | /* |
| 36 | 在Table View中每填充一个单元格的数据就会触发一次该事件。 |
| 37 | 注意:如果我们的数据一共有200项,并不代表会连续触发200次这个事件,如果当前屏幕只能显示10行数据的话,就只会触发10次该事件,当用户滚动该Table View而产生新的单元格时,才会继续触发该事件。 |
| 38 | */ |
| 39 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 40 | |
| 41 | static NSString *CellIndentifier = @"Contact"; |
| 42 | |
| 43 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier]; |
| 44 | /* |
| 45 | dequeueReusableCellWithIdentifier方法,获取UITableViewCell类型的对象,而且获取的是一个已经在TableView中使用过并可以复用的对象。 |
| 46 | 想象一下: |
| 47 | 如果数组中有1000个元素,我们为每一个元素都实例化一个UITableViewCell对象的话,系统就会内存溢出甚至崩溃。其实每个用户在一个屏幕中能够看到的单元格数量也就十几个,他们通过上下滚动屏幕的操作可以让一些已显示的单元格消除,而这些单元格对象系统就会保留下来以备我们需要显示新单元格时可以复用它们,从而达到了节省系统资源的目的。这个方法包含一个参数CellIdentifier, 它用于指明你需要哪个标识的可复用单元格。在同一界面中如果有多个表格的情况时非常有用。 |
| 48 | 当然如果没有获取到可复用的单元格时,我们就需要使用UITableViewCell的initWithStyle:reuseIdentifier:方法直接实例化一个单元格。其中reuseIdentifier参数用于设置该表格的可复用标识。 |
| 49 | */ |
| 50 | |
| 51 | if (cell == nil) { |
| 52 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease]; |
| 53 | |
| 54 | } |
| 55 | |
| 56 | NSString *cellValue = [listOfContacts objectAtIndex:indexPath.row]; |
| 57 | cell.textLabel.text = cellValue; |
| 58 | |
| 59 | //示意标志: Disclosure Indicator,Disclosure Button,Checkmark,默认为None |
| 60 | |
| 61 | //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 62 | |
| 63 | //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; |
| 64 | //cell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 65 | cell.accessoryType = UITableViewCellAccessoryNone; |
| 66 | |
| 67 | //单元格添加图片 |
| 68 | UIImage *image = [UIImage imageNamed:@"avatar.png"]; |
| 69 | cell.imageView.image = image; |
| 70 | |
| 71 | return cell; |
| 72 | } |
| 73 | //使用UITableViewDataSource协议的tableView:numberOfRowsInSection:方法 |
| 74 | //该方法用来设置Table View中要显示数据的行数 |
| 75 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 76 | return [listOfContacts count]; |
| 77 | } |
| 78 | //添加标题和脚本信息 |
| 79 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ |
| 80 | |
| 81 | return @"联系人列表"; |
| 82 | |
| 83 | |
| 84 | } |
| 85 | - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ |
| 86 | return @"作者:what if"; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | //UITableViewDelegate协议的方法,选择表格中的项目 |
| 91 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 92 | NSString *contactSelected = [listOfContacts objectAtIndex:[indexPath row]]; |
| 93 | NSString *msg = [[NSString alloc] initWithFormat:@"您选择的联系人:%@", contactSelected]; |
| 94 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择联系人" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; |
| 95 | [alert show]; |
| 96 | [alert release]; |
| 97 | [contactSelected release]; |
| 98 | [msg release]; |
| 99 | } |
| 100 | //UITableViewDelegate协议的方法,表格中的缩进 |
| 101 | - (NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 102 | return [indexPath row] % 9; |
| 103 | |
| 104 | } |
| 105 | - (void)dealloc{ |
| 106 | |
| 107 | |
| 108 | [listOfContacts release]; |
| 109 | [super dealloc]; |
| 110 | |
| 111 | } |
| 112 | - (void)viewDidUnload |
| 113 | { |
| 114 | [super viewDidUnload]; |
| 115 | } |
| 116 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
| 117 | { |
| 118 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
| 119 | } |
| 120 | @end |
二、通过plist文件提供数据,来显示数据,方便分组
1.添加contacts.plist文件
2. HomeViewController.h中添加代码
| 1 | #import <UIKit/UIKit.h> |
| 2 | @interface HomeViewController : UIViewController |
| 3 | <UITableViewDelegate, UITableViewDataSource>{ |
| 4 | } |
| 5 | @property (nonatomic, retain) NSDictionary *contactTitles;//存储所有的联系人信息 |
| 6 | @property (nonatomic, retain) NSArray *groups;//所有分类名称存入数组中 |
| 7 | @end |
3. HomeViewController.m中添加代码
| 1 | #import "HomeViewController.h" |
| 2 | @interface HomeViewController () |
| 3 | @end |
| 4 | @implementation HomeViewController |
| 5 | @synthesize contactTitles; |
| 6 | @synthesize groups; |
| 7 | - (void)viewDidLoad |
| 8 | { |
| 9 | |
| 10 | NSString *path = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];//plist文件路径 |
| 11 | NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; |
| 12 | self.contactTitles = dict; |
| 13 | [dict release]; |
| 14 | |
| 15 | NSArray *array = [[contactTitles allKeys] sortedArrayUsingSelector:@selector(compare:)]; |
| 16 | |
| 17 | self.groups = array; |
| 18 | [super viewDidLoad]; |
| 19 | } |
| 20 | //使用UITableViewDataSource协议的tableView:cellForRowAtIndexPath:方法 |
| 21 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 22 | |
| 23 | static NSString *CellIndentifier = @"Contact"; |
| 24 | |
| 25 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier]; |
| 26 | |
| 27 | if (cell == nil) { |
| 28 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease]; |
| 29 | |
| 30 | } |
| 31 | |
| 32 | |
| 33 | NSString *group = [groups objectAtIndex:[indexPath section]]; |
| 34 | NSArray * contactSection = [contactTitles objectForKey:group]; |
| 35 | cell.textLabel.text = [contactSection objectAtIndex:[indexPath row]]; |
| 36 | |
| 37 | |
| 38 | //单元格添加图片 |
| 39 | UIImage *image = [UIImage imageNamed:@"avatar.png"]; |
| 40 | cell.imageView.image = image; |
| 41 | |
| 42 | |
| 43 | return cell; |
| 44 | } |
| 45 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ |
| 46 | return [groups count]; |
| 47 | } |
| 48 | //使用UITableViewDataSource协议的tableView:numberOfRowsInSection:方法 |
| 49 | //该方法用来设置Table View中要显示数据的行数 |
| 50 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 51 | |
| 52 | NSString *group = [groups objectAtIndex:section]; |
| 53 | NSArray *contactSection = [contactTitles objectForKey:group]; |
| 54 | |
| 55 | return [contactSection count]; |
| 56 | } |
| 57 | //添加标题和脚本信息 |
| 58 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ |
| 59 | NSString *group = [groups objectAtIndex:section]; |
| 60 | return group; |
| 61 | |
| 62 | } |
| 63 | - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ |
| 64 | return @"作者:what if"; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /*//UITableViewDelegate协议的方法,选择表格中的项目 |
| 69 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 70 | NSString *contactSelected = [listOfContacts objectAtIndex:[indexPath row]]; |
| 71 | NSString *msg = [[NSString alloc] initWithFormat:@"您选择的联系人:%@", contactSelected]; |
| 72 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择联系人" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; |
| 73 | [alert show]; |
| 74 | [alert release]; |
| 75 | [contactSelected release]; |
| 76 | [msg release]; |
| 77 | } */ |
| 78 | /* |
| 79 | //UITableViewDelegate协议的方法,表格中的缩进 |
| 80 | - (NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 81 | return [indexPath row] % 9; |
| 82 | |
| 83 | }*/ |
| 84 | //索引功能 |
| 85 | |
| 86 | - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ |
| 87 | return groups; |
| 88 | } |
| 89 | //用户点击标志后触发的事件,只有DetailDisclosure Button才有该事件 |
| 90 | - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ |
| 91 | //进入到该项目的详细信息页面 |
| 92 | } |
| 93 | |
| 94 | |
| 95 | - (void)dealloc{ |
| 96 | |
| 97 | [contactTitles release]; |
| 98 | [groups release]; |
| 99 | [super dealloc]; |
| 100 | |
| 101 | } |
| 102 | - (void)viewDidUnload |
| 103 | { |
| 104 | [super viewDidUnload]; |
| 105 | } |
| 106 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
| 107 | { |
| 108 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
| 109 | } |
| 110 | @end |
4.在xib文件中修改其Style为Grouped
显示效果:

原文:http://www.cnblogs.com/nomine/p/3548636.html