您的位置:首页 > 移动开发 > IOS开发

iOS tableview用法

2015-10-22 00:00 375 查看
通过两种方法来实现:
一、通过动态数组NSMutableArray中的数据,来显示数据

1.新建Empty Application项目,新建ViewController,HomeViewController,在AppDelegate.m中导入该文件,
并在方法- (BOOL)application:didFinishLaunchingWithOptions:中添加以下红色标记的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor whiteColor];

HomeViewController *homeViewController = [[HomeViewController alloc] init];

self.window.rootViewController = homeViewController;

[homeViewController release];

[self.window makeKeyAndVisible];

return YES;

}

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中添加协议
#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController

<UITableViewDelegate, UITableViewDataSource>{

}

@end

目的:都添加协议,有备无患。提高代码编写的效率和可靠性。

3. 在HomeViewController.m中编写代码

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

NSMutableArray *listOfContacts;//声明动态数组

- (void)viewDidLoad

{

listOfContacts = [[NSMutableArray alloc] init];//分配内存并初始化

[listOfContacts addObject:@"张三"];

[listOfContacts addObject:@"张1"];

[listOfContacts addObject:@"张2"];

[listOfContacts addObject:@"张3"];

[listOfContacts addObject:@"张4"];

[listOfContacts addObject:@"张5"];

[listOfContacts addObject:@"张6"];

[listOfContacts addObject:@"张7"];

[listOfContacts addObject:@"张8"];

[listOfContacts addObject:@"张9"];

[listOfContacts addObject:@"张11"];

[listOfContacts addObject:@"张12"];

[listOfContacts addObject:@"张13"];

[listOfContacts addObject:@"张14"];

[listOfContacts addObject:@"张15"];

[listOfContacts addObject:@"张16"];

[listOfContacts addObject:@"张17"];

[listOfContacts addObject:@"张18"];

[listOfContacts addObject:@"张19"];

[super viewDidLoad];

}

//使用UITableViewDataSource协议的tableView:cellForRowAtIndexPath:方法

//该方法用来将数据填充进Table View的单元格中

/*

在Table View中每填充一个单元格的数据就会触发一次该事件。

注意:如果我们的数据一共有200项,并不代表会连续触发200次这个事件,如果当前屏幕只能显示10行数据的话,就只会触发10次该事件,当用户滚动该Table View而产生新的单元格时,才会继续触发该事件。

*/

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

static NSString *CellIndentifier = @"Contact";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];

/*

dequeueReusableCellWithIdentifier方法,获取UITableViewCell类型的对象,而且获取的是一个已经在TableView中使用过并可以复用的对象。

想象一下:

如果数组中有1000个元素,我们为每一个元素都实例化一个UITableViewCell对象的话,系统就会内存溢出甚至崩溃。其实每个用户在一个屏幕中能够看到的单元格数量也就十几个,他们通过上下滚动屏幕的操作可以让一些已显示的单元格消除,而这些单元格对象系统就会保留下来以备我们需要显示新单元格时可以复用它们,从而达到了节省系统资源的目的。这个方法包含一个参数CellIdentifier, 它用于指明你需要哪个标识的可复用单元格。在同一界面中如果有多个表格的情况时非常有用。

当然如果没有获取到可复用的单元格时,我们就需要使用UITableViewCell的initWithStyle:reuseIdentifier:方法直接实例化一个单元格。其中reuseIdentifier参数用于设置该表格的可复用标识。

*/

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];

}

NSString *cellValue = [listOfContacts objectAtIndex:indexPath.row];

cell.textLabel.text = cellValue;

//示意标志: Disclosure Indicator,Disclosure Button,Checkmark,默认为None
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

//cell.accessoryType = UITableViewCellAccessoryCheckmark;

cell.accessoryType = UITableViewCellAccessoryNone;

//单元格添加图片

UIImage *image = [UIImage imageNamed:@"avatar.png"];

cell.imageView.image = image;

return cell;

}

//使用UITableViewDataSource协议的tableView:numberOfRowsInSection:方法

//该方法用来设置Table View中要显示数据的行数

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

return [listOfContacts count];

}

//添加标题和脚本信息

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return @"联系人列表";

}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

return @"作者:what if";

}

//UITableViewDelegate协议的方法,选择表格中的项目

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSString *contactSelected = [listOfContacts objectAtIndex:[indexPath row]];

NSString *msg = [[NSString alloc] initWithFormat:@"您选择的联系人:%@", contactSelected];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择联系人" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alert show];

[alert release];

[contactSelected release];

[msg release];

}

//UITableViewDelegate协议的方法,表格中的缩进

- (NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

return [indexPath row] % 9;

}

- (void)dealloc{

[listOfContacts release];

[super dealloc];

}

- (void)viewDidUnload

{

[super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

@end

二、通过plist文件提供数据,来显示数据,方便分组
1.添加contacts.plist文件
2.
HomeViewController.h中添加代码
#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController

<UITableViewDelegate, UITableViewDataSource>{

}

@property (nonatomic, retain) NSDictionary *contactTitles;//存储所有的联系人信息

@property (nonatomic, retain) NSArray *groups;//所有分类名称存入数组中

@end

3. HomeViewController.m中添加代码

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

@synthesize contactTitles;

@synthesize groups;

- (void)viewDidLoad

{

NSString *path = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];//plist文件路径

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

self.contactTitles = dict;

[dict release];

NSArray *array = [[contactTitles allKeys] sortedArrayUsingSelector:@selector(compare:)];

self.groups = array;

[super viewDidLoad];

}

//使用UITableViewDataSource协议的tableView:cellForRowAtIndexPath:方法

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

static NSString *CellIndentifier = @"Contact";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];

}

NSString *group = [groups objectAtIndex:[indexPath section]];

NSArray * contactSection = [contactTitles objectForKey:group];

cell.textLabel.text = [contactSection objectAtIndex:[indexPath row]];

//单元格添加图片

UIImage *image = [UIImage imageNamed:@"avatar.png"];

cell.imageView.image = image;

return cell;

}

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

return [groups count];

}

//使用UITableViewDataSource协议的tableView:numberOfRowsInSection:方法

//该方法用来设置Table View中要显示数据的行数

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

NSString *group = [groups objectAtIndex:section];

NSArray *contactSection = [contactTitles objectForKey:group];

return [contactSection count];

}

//添加标题和脚本信息

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

NSString *group = [groups objectAtIndex:section];

return group;

}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

return @"作者:what if";

}

/*//UITableViewDelegate协议的方法,选择表格中的项目

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSString *contactSelected = [listOfContacts objectAtIndex:[indexPath row]];

NSString *msg = [[NSString alloc] initWithFormat:@"您选择的联系人:%@", contactSelected];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择联系人" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alert show];

[alert release];

[contactSelected release];

[msg release];

} */

/*

//UITableViewDelegate协议的方法,表格中的缩进

- (NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

return [indexPath row] % 9;

}*/

//索引功能
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

return groups;

}

//用户点击标志后触发的事件,只有DetailDisclosure Button才有该事件

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

//进入到该项目的详细信息页面

}

- (void)dealloc{

[contactTitles release];

[groups release];

[super dealloc];

}

- (void)viewDidUnload

{

[super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

@end

4.在xib文件中修改其Style为Grouped

备注:
修改单元格的方法:
textLabel.font 使用UIFont设置单元格标签的字体
textLabel.lineBreakMode 使用UILineBreakMode指定单元格标签的文本如何换行
textLabel.text 将单元格标签的内容设置为一个NSString
textLabel.textAlignment 使用UITextAlignment设置单元格标签文本的对齐方式
textLabel.textColor 使用UIColor设置单元格标签文本的颜色
textLabel.selectedTextColor 使用UIColor设置选定文本的颜色
imageView.image 将单元格的图像视图设置为一个UIImage
imageView.selectedImage 将选定单元格的内容设置为UIImage
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: