IOS学习之路五(代码实现UITableView)
来源:互联网 发布:北京体检数据 编辑:程序博客网 时间:2024/06/06 08:23
先展示一下运行结果:
代码实现:
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
- IOS学习之路五(代码实现UITableView)
- IOS学习之路五(代码实现UITableView)
- IOS学习之UITableView
- IOS学习之 UITableView
- IOS学习之UITableView
- IOS学习之路十二(UITableView下拉刷新页面)
- IOS学习之路十二(UITableView下拉刷新页面)
- IOS控件学习之UITableView代码实例详解
- iOS学习笔记之UITableView(1)
- IOS学习之——UITableView 实现数据源的方法
- ios学习之UITableView(一)
- IOS UI学习之UITableView
- iOS开发学习之UITableView
- iOS之UITableView之样板代码
- IOS学习之UiTableView下拉刷新与自动加载更多,百年不变的效果(五)
- iOS学习之路-简易”IT之家“(UITableView)
- IOS-UITableView上的纯代码实现
- IOS 代码优化之 整洁的UITableView
- 浅析传统关系数据库面临大数据的挑战
- 【读书笔记】怪诞行为学
- 初识三层架构
- poj 3667 Hotel
- 刘汝佳黑书 pku等oj题目
- IOS学习之路五(代码实现UITableView)
- 在Linux下使用sprintf代替atoi实现整型转化为char*
- sscanf()函数用法
- 合并排序
- 【面试】:数组和链表的区别
- 为Galaxy nexus 编译官方Android源代码---以及下载官网binary驱动
- 【读书笔记】黑天鹅 如何应对不可预知的未来
- Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
- java程序员面试宝典(1)