IOS学习之路五(代码实现UITableView)
先展示一下运行结果:
代码实现:
1.先创建一个空项目:
2.创建一个Controller:(TableViewController)
在AppDelegate.h中声明属性:
// AppDelegate.h // UITableViewDemo // // Created by WildCat on 13-8-6. // Copyright (c) 2013年 wildcat. All rights reserved. // #import <UIKit/UIKit.h> @class TableViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (nonatomic,strong) TableViewController *tableViewController; @property (strong, nonatomic) UIWindow *window; @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; - (void)saveContext; - (NSURL *)applicationDocumentsDirectory; @end
在.m文件中实现:关键代码:
#import "AppDelegate.h" #import "TableViewController.h" @implementation AppDelegate @synthesize tableViewController=_tableViewController; @synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; self.tableViewController=[[TableViewController alloc] initWithNibName:nil bundle:NULL]; [self.window addSubview:self.tableViewController.view]; return YES; }
在TableViewController中声明:
#import <UIKit/UIKit.h> @interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> @property (nonatomic, strong) UITableView *myTableView; @end
在其.m文件中实现:
// // TableViewController.m // UITableViewDemo // // Created by WildCat on 13-8-6. // Copyright (c) 2013年 wildcat. All rights reserved. // #import "TableViewController.h" @interface TableViewController () @end @implementation TableViewController @synthesize myTableView; #pragma mark - 实现UITableViewDelegate的方法 //当cell被点击时调用该方法 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([tableView isEqual:self.myTableView]){ NSLog(@"%@", [NSString stringWithFormat:@"Cell %ld in Section %ld is selected", (long)indexPath.row, (long)indexPath.section]); } } - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ CGFloat result = 20.0f; if ([tableView isEqual:self.myTableView]){ result = 40.0f; } return result; } #pragma mark - 实现datasource的方法 //确定section的个数 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ NSInteger result=0; if ([tableView isEqual:self.myTableView]) { result=3; } return result; } //每个section的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSInteger result = 0; if ([tableView isEqual:self.myTableView]){ switch (section){ case 0:{ result = 3; break; } case 1:{ result = 5; break; } case 2:{ result = 8; break; } } } return result; } //设置每个静态的cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *result = nil; if ([tableView isEqual:self.myTableView]){ static NSString *TableViewCellIdentifier = @"MyCells"; result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier]; if (result == nil){ result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier]; } result.textLabel.text = [NSString stringWithFormat:@"Section %ld, Cell %ld", (long)indexPath.section, (long)indexPath.row]; // result.backgroundColor=[UIColor greenColor]; //背景色 result.detailTextLabel.text = @"详细内容"; if (indexPath.section==0) { result.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //设置右边的样式 }else if(indexPath.section==1){ result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; //设置右边的样式 }else{ result.accessoryType = UITableViewCellAccessoryCheckmark; //设置右边的样式 } /* 这两个附件的区别在于 disclosure indicator 不产生事件,而 detail disclosure 按钮在被点击时会向委托触发一个事件;也就是说,点击 cell 上的按键会 有不同效果。因此,detail disclosure 按钮允许用户在同一行上执行两个独立但相关的操 作。 */ // typedef enum { // UITableViewCellAccessoryNone, // don't show any accessory view // UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track // UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks // UITableViewCellAccessoryCheckmark // checkmark. doesn't track // } UITableViewCellAccessoryType; } return result; } //实现DetailDisclosure被点击出发的方法 - (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ /* Do something when the accessory button is tapped */ NSLog(@"Accessory button is tapped for cell at index path = %@", indexPath); UITableViewCell *ownerCell = [tableView cellForRowAtIndexPath:indexPath]; NSLog(@"Cell Title = %@", ownerCell.textLabel.text); } #pragma mark - TableViewController 的方法 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];//另一种样式:UITableViewStyleGrouped self.myTableView.dataSource = self; self.myTableView.delegate = self; self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight; [self.view addSubview:self.myTableView]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. self.myTableView = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end