UI_UITableView
来源:互联网 发布:游戏编程模式pdf 网盘 编辑:程序博客网 时间:2024/06/01 09:13
RootViewController.m#import "RootViewController.h"#define WIDTH self.view.frame.size.width#define HEIGHT self.view.frame.size.height@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic, retain)NSMutableArray *arr;@end@implementation RootViewController1.数组初始化- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil]; } return self;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; self.navigationController.navigationBar.translucent = NO; 2.当页面只有一个tableview能滚动的视图时,导航栏会自动把坐标系的起点放到(0, 64)的位置,既能保留半透明效果,也方便视图的布局. self.automaticallyAdjustsScrollViewInsets = YES; 3.创建一个UITableView UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain]; tableView.backgroundColor = [UIColor greenColor]; [self.view addSubview:tableView]; [tableView release]; 4.设置行高. tableView.rowHeight = 100; 5.签订代理人.dataSource tableView.dataSource = self; 6.设置代理人. tableView.delegate = self;}
7.这个是dataSource实现的第一个协议方法,放回每个分区有多少行,分区默认是1个.- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section % 2 == 0) { return self.arr.count; } else { return 10; } // 数组元素个数跟行数进行关联. }
8.只要操作tableview就会触发这个方法,完成对cell的创建和赋值的作用,并且重用的方式完成cell的创建和取用.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 1.指定一个重用标志的字符串. static NSString *reuse = @"reuse"; 2.tableview根据指定的重用标志,在重用池里寻找有没有闲置的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; 3.如果没有闲置的cell,需要创建一个cell if (!cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];// NSLog(@"我被创建了"); } 4.cell创建好之后对cell上的属性进行赋值. cell.textLabel.text = self.arr[indexPath.row]; cell.detailTextLabel.text = @"2"; cell.imageView.image = [UIImage imageNamed:@"soldier.jpg"];// NSLog(@"%ld", indexPath.row); return cell;}
9.tableView上的索引.- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return @[@"A", @"B", @"C"];}
10.指定当前的分区个数.- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 5;}
11.指定每个分区的标题.- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (section == 0) { return @"潘金莲"; } else if (section == 1) { return @"阎婆惜"; } { return @"西门庆"; }}
12.如果想写自定义标题,就要和上面的方法一起写,缺一不可.- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 24)] autorelease]; headerView.backgroundColor = [UIColor clearColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(300, 0, 50, 24); [button setTitle:@"更多" forState:UIControlStateNormal]; [headerView addSubview:button]; return headerView;}
13.tableview的点击方法.- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"当前行数为:%ld", indexPath.row); NSLog(@"当前分区数为:%ld", indexPath.section); NSLog(@"当前点击人的姓名为:%@", self.arr[indexPath.row]); UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSLog(@"当前的cell为:%@", cell.textLabel.text);}
14.返回行高.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row % 2 == 0) { return 150; } else { return 100; }}
15.设置分区标题高度.- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section % 2 == 0) { return 50; } else { return 10; }}
0 0
- UI_UITableView
- UI_UITableView
- UI_UITableView编辑
- UI_UITableView编辑
- UI_UITableView 编辑与移动
- UI_UITableView异步加载图片
- UI_UITableView _新知识_02
- UI_UITableView自定义Cell
- UI_UITableView省市区字典数组
- UI_UITableView的HeaderView
- UI_UITableView界面传值(将前面的属性传值和协议传值结合起来)
- 找出数组中第K大的数
- 剑指offer—变态跳台阶
- php include(),include_once(),require(),require_once()函数
- 第4章 IP协议
- 基于ActiveMQ的消息中间件系统 OneMM逻辑与物理架构设计详解
- UI_UITableView
- 实现单链表的排序(递增)
- 剑指offer—矩形覆盖
- 歪果仁博客和Twitter账户
- 自己写一个程序 快速的删除文件
- <Android知识点整理>2.Android常用的布局
- LintCode -- 二叉树的前序遍历
- BZOJ 2300 [HAOI2011]防线修建 Splay维护动态凸包
- pat 1098 Insertion or Heap Sort