主要目的:熟悉TableView的内部属性和方法,掌握TableView的使用套路。
例子1、直接使用TableView来展示字符集所有字体。
简要步骤:
1、新建一个Root01VIewController的OC类,继承于UIViewController,在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中加入初始化的逻辑,具体如下:
- (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]; [self.window makeKeyAndVisible]; Root01ViewController *root01ViewCtrl = [[Root01ViewController alloc] init]; UINavigationController *navCtrl =[[UINavigationController alloc] initWithRootViewController:root01ViewCtrl]; self.window.rootViewController = navCtrl; [root01ViewCtrl release]; [navCtrl release]; return YES; }
2、为Root01ViewController添加实现的协议(UITableVIewDatasource)、添加属性tableView
@interface Root01ViewController : UIViewController<UITableViewDataSource> { @private UITableView *_tableView; @private NSArray *_dataArray; }
3、实现协议的2个必须的方法和一个可选方法
#pragma -mark delegate method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_dataArray count]; } // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"mycell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if(cell == nil){ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; } cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row]; return cell; } //@optional // Default is 1 if not implemented - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; }
4、编写Root01VIewController的loadView方法
- (void)loadView { UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; self.view = view; [view release]; _dataArray = [[UIFont familyNames] retain]; _tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain]; _tableView.dataSource = self; [self.view addSubview:_tableView]; //[_tableView release]; /*1、self.view不能为空,必须先赋值,否则会报错。 2、addSubview:_tableView之后需要在dealloc中release一次*/ }
5、进行测试
6、添加背景图片,设置单元格的统一高度,设置表视图的背景颜色、分割线的颜色和分割线的风格
/*add background Image*/ UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableView_background"]]; _tableView.backgroundView = imageView; _tableView.rowHeight = 80;/*default is 44px*/ // 设置表视图的颜色 _tableView.backgroundColor = [UIColor yellowColor]; // 设置表视图的分割线的颜色 _tableView.separatorColor = [UIColor purpleColor]; // 设置表视图的分割线的风格 _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
7、添加表视图的头部
// 设置表视图的头部视图(headView 添加子视图)
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
headerView.backgroundColor = [UIColor redColor];
// 添加子视图
UILabel *headText = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 200, 80)];
headText.text = @"天晴朗,天晴朗天晴朗天晴朗!";
headText.numberOfLines = 0;
[headerView addSubview:headText];
[headText release];
_tableView.tableHeaderView = headerView;
[headerView release];
8、添加表视图的尾部
// 设置表视图的尾部视图(footerView 添加子视图)
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
footerView.backgroundColor = [UIColor yellowColor];
_tableView.tableFooterView = footerView;
[footerView release];