您的位置:首页 > 产品设计 > UI/UE

UI08_UItableView(关于cell滑动条消失,隐藏cell线,解决cell的线不对应问题)

2015-08-08 15:44 441 查看
初始化设置(需要我们输入相关的内容时我们是需要设置)

-(instancetype)initWithNibName:(NSString *)nibNameOrNilboudle:(NSBundle *)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
}
return self;
}


这里我们还是将导航视图控制器的视图设置成不透明色
self.navigationController.navigationBar.translucent=NO;
设置标题
self.title=@"标题";


点击cell不变色

cell.selectionStyle = UITableViewCellSelectionStyleNone;


关于cell滑动条消失

self.healthAndTatooTableView.showsVerticalScrollIndicator = NO;


隐藏cell线

self.healthAndTatooTableView.separatorStyle = UITableViewCellSeparatorStyleNone;


解决cell的线不对应问题

在自定义的cell里我们要在layoutSubview里要写上继承父类的程序,要不就是覆盖父类了


1.创建UITableView

UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-64)style:UITableViewStylePlain];


2.设置背景颜色

tableView.backgroundColor=[UIcolor yellowColor];


3.添加到视图控制器视图上并释放

[self.view addSubview:tableView];
[tableView release];


4.设置行高

tableView.rowHeight=100;


5.设置分区数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}


6.设置section的头标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"方式;ofi";
}


7.相当于索引条,直接引入分区的位置

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [NSMutableArray arrayWithObjects:@"1", @"2",  nil];
}
//设置多少分区数组就有多少元素  多了也没用


8.给tableView添加头视图

首先定义一个imageView,为了方便我们将他设置成属性特性
self.imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"h5.jpeg"]];
self.imageView.frame=CGRectMake(0, 0-200, self.view.frame.size.width, 200);
//接下来添加视图有两种方法
第一种
tableView.tableHeardView=self.imageView;
第二种
宽是tableView的宽度,
[tableView addSubview:self.imageView];
上面设置imageView为什么减200,因为被盖住了   所以向下推200高度
tableView.contentInset=UIEdgeinsetMake(200,0,0,0);
这个坐标的显示的是上左下右的填写坐标点


9.往下拽的时候让图片均匀放大

#pragma mark  tableView的delegate已经签订好scrollVIew的协议,只要设置好代理人,就可以使用scroVIew的协议方法
//只要滑动就会触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"ddddd");
//    获取偏移量
CGFloat y=scrollView.contentOffset.y;
NSLog(@"%g",y);
if (y<0) {
self.imageView.frame=CGRectMake(0, y, self.view.frame.size.width, -y);
}
}


10.特有的两套代理方法

与系统签订协议:
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>


UITableViewDataSource协议(有两个必须实现的方法)

第一套协议必须是实现的方法
(1)设置代理人
在viewDidLoad里设置代理人
(2)该协议必须实现的方法一
-(NSIteger)tableView:(UITableView *)tableViewn numberOfRowsInSection:(NSInteger)section
{
//让数组元素的个数和行数保持相同
return self.arr.count;
奇数分区是5行   偶数分区是10行
先执行设置分区的方法,后执行每个分区有多少行
if(section%2==1)
{
return 5;
}else
{
return 10;
}
}


第二套协议必须实现的方法
该协议主要是为了显示数据   在每一行上添上一个cell,高是每一行的高度,宽是屏幕宽,存放是一次存放,从第一个开始.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
给重用池设置一个重用的标志,根据这个标志找到对应的重用池
static NSString *reuse=@"reuse";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];(1)
if (!cell)
{
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1  reuseIdentifier:reuse]autorelease];(2)
}
(3)
cell.textLabel.text=self.arr[indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:@"%ld",indexPath.section];
cell.imageView.image=[UIImage imageNamed:@"h1.jpeg"];
indexpath有两个内容,一个是indexpath.row,表示它是第几行既行数,从零开始计算
NSLog(@"%ld",indexPath.row);
return cell;
}
解释:
static特点
1.只初始化一次
2.如果没有初始值,默认是0;
3.直到程序结束才会消失
将reuse设为重用池,每一个cell甚至是tableView都有自己的重用池,二维不重复
cell显示结束后,会把cell统一的放到重用池中,等需要的cell显示了,先从重用池中找,看有没有空闲的cell.如果有的话就用空闲的,如果没有再创建.
cell重用的目的是为了节约成本,用有限的cell把所有数据都显示出来
(1)tableView根据重用的标志找重用池,看里面是够有空闲的cell,如果有的话cell会保存一个有效的地址,就是一个正常的堆空间的地址;没有的话cell的地址就是空nil,此时将会执行if语句
(2)如果没有,执行if并需要创建cell.!cell与cell==nil;相同
(3)对cell进行赋值;cell里有默认的三个控件,imageView和两个label


UITableViewDelegate(有tableView的点击方法)—这里面没有必须实现的方法

找到当前鼠标点击的位置,行和区.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"section:%ld,row:%la",indexPath.section,indexP-
ath.row);
NSLog(@"人名:%@",self.arr[indexPath.row]);
//点击之后推出下一页
SecondViewController *second=[[SecondViewController alloc] init];
[self.navigationController pushViewController:second animated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: