通过两种方法来实现:
一、通过动态数组NSMutableArray中的数据,来显示数据
1.新建Empty Application项目,新建ViewController,HomeViewController,在AppDelegate.m中导入该文件,并在方法- (BOOL)application:didFinishLaunchingWithOptions:中添加以下红色标记的代码。
View Row Code
1- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions2{3self.window=[[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];45self.window.backgroundColor=[UIColorwhiteColor];67HomeViewController*homeViewController=[[HomeViewControlleralloc]init];8self.window.rootViewController= homeViewController;9[homeViewControllerrelease];1011[self.windowmakeKeyAndVisible];12returnYES13}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中添加协议:
View Row Code
1#import <UIKit/UIKit.h>2@interfaceHomeViewController : UIViewController3<UITableViewDelegate,UITableViewDataSource>{4}5@end目的:都添加协议,有备无患。提高代码编写的效率和可靠性。
3. 在HomeViewController.m中编写代码
View Row Code
1#import"HomeViewController.h"2@interfaceHomeViewController ()3@end4@implementationHomeViewController5NSMutableArray*listOfContacts;6- (void)viewDidLoad7{89listOfContacts=[[NSMutableArrayalloc]init];1011[listOfContactsaddObject:@"张三"];12[listOfContactsaddObject:@"张1"];13[listOfContactsaddObject:@"张2"];14[listOfContactsaddObject:@"张3"];15[listOfContactsaddObject:@"张4"];16[listOfContactsaddObject:@"张5"];17[listOfContactsaddObject:@"张6"];18[listOfContactsaddObject:@"张7"];19[listOfContactsaddObject:@"张8"];20[listOfContactsaddObject:@"张9"];21[listOfContactsaddObject:@"张11"];22[listOfContactsaddObject:@"张12"];23[listOfContactsaddObject:@"张13"];24[listOfContactsaddObject:@"张14"];25[listOfContactsaddObject:@"张15"];26[listOfContactsaddObject:@"张16"];27[listOfContactsaddObject:@"张17"];28[listOfContactsaddObject:@"张18"];29[listOfContactsaddObject:@"张19"];3031[superviewDidLoad];32}33343536373839- (UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{4041staticNSString*CellIndentifier=@"Contact";4243UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:CellIndentifier];4445464748495051if (cell==nil) {52cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIndentifier]autorelease];5354}5556NSString*cellValue=[listOfContactsobjectAtIndex:indexPath.row];57cell.textLabel.text= cellValue;5859606162636465cell.accessoryType=UITableViewCellAccessoryNone;666768UIImage*image=[UIImageimageNamed:@"avatar.png"];69cell.imageView.image= image;7071return cell;72}737475- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{76return[listOfContactscount];77}7879- (NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{8081return@"联系人列表";828384}85- (NSString*)tableView:(UITableView*)tableViewtitleForFooterInSection:(NSInteger)section{86return@"作者:what if";87}88899091- (void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{92NSString*contactSelected=[listOfContactsobjectAtIndex:[indexPathrow]];93NSString*msg=[[NSStringalloc]initWithFormat:@"您选择的联系人:%@",contactSelected];94UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"选择联系人"message:msgdelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];95[alertshow];96[alertrelease];97[contactSelectedrelease];98[msgrelease];99}100101- (NSInteger)tableView:(UITableView*)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath*)indexPath{102return[indexPathrow]%9;103104 105- (void)dealloc{106107108[listOfContactsrelease];109[superdealloc];110111}112- (void)viewDidUnload113{114[superviewDidUnload];115}116- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation117{118return (interfaceOrientation==UIInterfaceOrientationPortrait);119}120@end二、通过plist文件提供数据,来显示数据,方便分组
1.添加contacts.plist文件
2. HomeViewController.h中添加代码
View Row Code
1#import <UIKit/UIKit.h>2@interfaceHomeViewController : UIViewController3<UITableViewDelegate,UITableViewDataSource>{4}5@property (nonatomic, retain)NSDictionary*contactTitles;6@property (nonatomic, retain)NSArray*groups;7@end3. HomeViewController.m中添加代码
View Row Code
1#import"HomeViewController.h"2@interfaceHomeViewController ()3@end4@implementationHomeViewController5@synthesize contactTitles;6@synthesize groups;7- (void)viewDidLoad8{910NSString*path=[[NSBundlemainBundle]pathForResource:@"contacts"ofType:@"plist"];11NSDictionary*dict=[[NSDictionaryalloc]initWithContentsOfFile:path];12self.contactTitles= dict;13[dictrelease];1415NSArray*array=[[contactTitlesallKeys]sortedArrayUsingSelector:@selector(compare:)];1617self.groups= array;18[superviewDidLoad];19}2021- (UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{2223staticNSString*CellIndentifier=@"Contact";2425UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:CellIndentifier];2627if (cell==nil) {28cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIndentifier]autorelease];2930}313233NSString*group=[groupsobjectAtIndex:[indexPathsection]];34NSArray* contactSection=[contactTitlesobjectForKey:group];35cell.textLabel.text=[contactSectionobjectAtIndex:[indexPathrow]];36373839UIImage*image=[UIImageimageNamed:@"avatar.png"];40cell.imageView.image= image;414243return cell;44}45- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{46return[groupscount];47}484950- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{5152NSString*group=[groupsobjectAtIndex:section];53NSArray*contactSection=[contactTitlesobjectForKey:group];5455return[contactSectioncount];56}5758- (NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{59NSString*group=[groupsobjectAtIndex:section];60return group;6162}63- (NSString*)tableView:(UITableView*)tableViewtitleForFooterInSection:(NSInteger)section{64return@"作者:what if";65}666768697071727374757677787980818283848586- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView{87return groups;88}8990- (void)tableView:(UITableView*)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath{9192}939495- (void)dealloc{9697[contactTitlesrelease];98[groupsrelease];99[superdealloc];100101}102- (void)viewDidUnload103{104[superviewDidUnload];105}106- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation107{108return (interfaceOrientation==UIInterfaceOrientationPortrait);109}110@end4.在xib文件中修改其Style为Grouped
显示效果:
![iOS TableView](http://pic.9sssd.com/Img/2012/10/29/12995973603661721577085.png)
开发多个Section的tableView。
首先应该考虑到数据源该如何得到
我们这里可以通过两种方式:第一种是读取plist文件。第二种是通过代码进行数据存储以及读取。
多个Section需要的数据源是一个字典,字典里的内容是一个数组。在plist文件中可以这样去创建
![](http://img.blog.csdn.net/20130614153854890)
在.h文件中定义一个字典,一个数组
![](http://img.blog.csdn.net/20130614153942390)
在.m文件的viewDidLoad函数中:
![](http://img.blog.csdn.net/20130614154012500)
解释一下:通过bundle得到整个程序的沙盒,新建NSURL对象,读取到一个字典中,然后取所有的字典key存储到一个数组中。
接下来就是实现TableView的代理和数据源函数啦
![](http://img.blog.csdn.net/20130614154802015)
section数量取的是字典重元素的count值;
每个section的行数区的是相对应的字典元素数组的count值.
cellForRowAtIndexPath中的内容就直接取值显示。
下面要对tableView每个section加标题:
![](http://img.blog.csdn.net/20130614154859718)
下面是要增加tableview右侧的索引栏:
![](http://img.blog.csdn.net/20130614154927328)
效果也就完成啦:
看看效果吧
右面是拉动索引栏时的效果图。
![](http://img.blog.csdn.net/20130614155043640)
![](http://img.blog.csdn.net/20130614155010718)