UITableView
来源:互联网 发布:2015年程序员考试真题 编辑:程序博客网 时间:2024/04/25 03:47
UITableView
- 表视图的每一条数据都是显示在UITableViewCell对象中
分区显示数据, 每一个分区称为一个section, 每一行称为row, 编号都是从0开始
DataSource数据源
-我们需要给tableView指定一个数据源, 它负责给tableView提供数据, 需要实现协议中两个必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSinteger)section- (UITableVIewCell *)tableView:(UItableView *)tableView cellForRowARIndexPath:(NSIndexPath *)indexPath
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
AppDelegate.m 文件
#import "AppDelegate.h"#import "MainViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (void)dealloc{ [_window release]; [super dealloc];}- (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]; [_window release]; MainViewController *mainVC = [[MainViewController alloc]init]; UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:mainVC]; self.window.rootViewController = naVC; [mainVC release]; [naVC release]; return YES;}
MainViewController.m 文件
#import "MainViewController.h"#import "SecondViewController.h"@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>@property(nonatomic, retain)NSMutableArray *arr;@end@implementation MainViewController- (void)dealloc{ [_arr release]; [super dealloc];}// 重写初始化方法- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ // 方法1 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.arr = [NSMutableArray arrayWithObjects:@"宋江1", @"卢俊义2", @"吴用3", @"公孙胜4", @"关胜5", @"林冲6", @"秦明7" ,@"呼延灼8" , @"花容9",@"柴进10", @"李应11", @"朱仝12",@"鲁智深13",@"武松14",nil]; } // 方法2 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self createData]; } return self;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor orangeColor]; self.navigationController.navigationBar.translucent = NO; self.title = @"表视图";// 创建UITableView (继承于UIScrollView 可以滚动)// UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; UITableView *tableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain]; tableView.backgroundColor = [UIColor orangeColor]; [self.view addSubview:tableView]; [tableView release];// 设置行高 tableView.rowHeight = 100;// tableView的两套代理 tableView.dataSource = self; tableView.delegate = self;}#pragma mark tableview里有多少个section (分区), 默认1个分区- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 10;}// #pragma mark tableview第一个必须实现的协议方法, 指定分区内有多少行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 方法1:让数组里的元素个数和行数保持相同 //return self.arr.count; // 奇数分区有5行, 偶数分区有10行 // 先执行分区的方法, 后执行每个分区里有多少行 if(section % 2 == 1){ return 5; }else{ return 10; }}// #pragma mark 第二个协议方法, 主要是用来显示数据 (每一分区每一行显示的数据)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{// static特点: // 1.只初始化一次 // 2.如果没有初始值, 默认是0; // 3.指导程序结束, 才会消失 // 创建cell // 当cell 显示结束之后, 会把cell统一的放到重用池中, 等需要cell显示了, 先从重用池中找, 看有没有闲置的cell, 如果有的话就用闲置的cell, 如果没有再创建 // cell的重用是为了节约创建成本, 用有限的cell把所有的数据显示出来 // 1.给重用池设置一个重要标志, 根据这个标志可以找到对应的重用池 static NSString *reuse = @"reuse"; // 2.tableView通过重用标志在重用池中寻找cell, 如果有闲置的cell, cell会保存一个有效的cell对象地址, 如果没有, cell里面则是nil, 空 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; // 3.如果没有cell, 则进行cell的创建 if (!cell) { cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease]; } // 对cell进行赋值 // celll里有默认的三个控件 // 1.大标题 cell.textLabel.text = @"宋江"; cell.textLabel.text = self.arr[indexPath.row]; // 2.小标题 cell.detailTextLabel.text = @"卢俊义"; cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", indexPath.section]; // 3.图片框 cell.imageView.image = [UIImage imageNamed:@"8.jpg"]; //indexPath.row保存的是行数 return cell;}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{return @"水浒";}#pragma mark 索引- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",];}//*************<UITableViewDelegate>********************************************- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"section:%ld, row:%ld", indexPath.section, indexPath.row); // 打印当前点击的人名是什么 NSLog(@"%@", self.arr[indexPath.row]); // 点击之后推出下一页 SecondViewController *secondVC = [[SecondViewController alloc]init]; [self.navigationController pushViewController:secondVC animated:YES]; [secondVC release];}
给tableView添加头视图, 下拉时头视图变大
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"主界面"; self.navigationController.navigationBar.translucent = NO; self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain]; self.tableView.backgroundColor = [UIColor orangeColor]; [self.view addSubview:self.tableView]; [_tableView release]; self.tableView.rowHeight = 100; self.tableView.dataSource = self; self.tableView.delegate = self; self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"10.jpg"]]; self.imageView.frame = CGRectMake(0, 0 -200 , self.view.frame.size.width, 200); //给tableView添加头视图 (两种效果 对比) //self.tableView.tableHeaderView = self.imageView; // 宽是tableView的宽度 [self.tableView addSubview:self.imageView]; self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);}#pragma mark tableview的delegate已经签订好scrollview的协议, 只要设置好代理人, 就可以使用scrollView的协议方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ //NSLog(@"滑动"); //获取偏移量 CGFloat y = scrollView.contentOffset.y; NSLog(@"%g", y); if (y < 0) { self.imageView.frame = CGRectMake(0, y, self.view.frame.size.width, -y); }}
界面传值 (后向前)
SecondViewController.h 文件
#import <UIKit/UIKit.h>// 1.声明一份协议@protocol SecondViewControllerDelegate <NSObject>- (void)changeValue:(NSString *)str;@end@interface SecondViewController : UIViewController// 2.设置代理人属性@property(nonatomic, assign)id<SecondViewControllerDelegate>delegate;@property(nonatomic, copy)NSString *name;@end
SecondVIewController.m文件
- (void)click:(UIButton *)button{ // 3. [self.delegate changeValue:self.textfield.text]; [self.navigationController popToRootViewControllerAnimated:YES];}
MainViewController.m 文件
// 4.签协议@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate, SecondViewControllerDelegate>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ SecondViewController *secVC = [[SecondViewController alloc]init]; [self.navigationController pushViewController:secVC animated:YES]; [secVC release]; secVC.name = self.arr[indexPath.row]; // 5. 设置代理人 secVC.delegate = self;}// 6.-(void)changeValue:(NSString *)str{// 属性的数组, 相当于数据源, 把传过来的值添加到数组中 [self.arr addObject:str];// 对tableview进行刷新操作 [self.tableView reloadData];}
0 0
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableview
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView,
- UITableView
- UITableView
- UITableView
- UITableView
- 99% 的人中枪:过度分析
- UITableView省市区数组
- 3.2Library string Type
- hdoj1561The more, The Better【树形dp+01背包】
- 设计模式之-----命令模式
- UITableView
- LeetCode(151) Reverse Words in a String
- 单选框、多选框的在线填写写法
- DSP28335硬件教程-xintf接口
- 利用Bundle来封装数据,然后用intent传递
- ubuntu下sts不显示菜单
- 借助motion操控Linux下的摄像头
- 函数返回局部变量
- ACE_NEW开头的宏定义