美文网首页
UITableView

UITableView

作者: nothing_c | 来源:发表于2016-11-01 00:02 被阅读52次

插入行。删除行。折叠区。设置索引。刷新表格。注册cell。


//UITableView的ADD

// NSString * name = @"new";

//NSInteger section = indexPath.section;

NSMutableArray * newArray = [dataArray objectAtIndex:indexPath.section];

[newArray addObject:dataArray [indexPath.section] [indexPath.row]];

//NSInteger row =  newArray.count - 1;

NSIndexPath * path = [NSIndexPath indexPathForRow:newArray.count- 1 inSection:indexPath.section];

[tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];

//UITableView的DELETE

NSMutableArray * lastArray =dataArray [indexPath.section];

if(lastArray.count> 0) {

[lastArray removeObjectAtIndex:indexPath.row];

}

[tableView reloadData];

UITableView的cell折叠

#import"ViewController.h"

#import"customTableViewCell.h"

@interfaceViewController() {

UITableView * table;

BOOL flag[3];

}

@end

@implementation ViewController

//释放全局变量

-(void)dealloc {

[table release];

//继承父类

[super dealloc];

}

- (void)viewDidLoad {

[super viewDidLoad];

//全局的UITableView

table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];

//UITableView代理

table.delegate = self;

table.dataSource = self;

//第二种注册方式记得创建继承于UITableViewCell的类下面创建cell时要注意类名称

[table registerClass:[customTableViewCell class] forCellReuseIdentifier:@"cell"];

//系统自动偏移属性

self.automaticallyAdjustsScrollViewInsets = NO;

//添加

[self.view addSubview:table];

}

//返回区

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 3;

}

//返回行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

//默认折叠开关是关闭状态

if(flag[section] == NO) {

//关闭状态下行数为0

return 0;

} else {

//点击打开后行数为5

return 5;

}

}

//重用机制方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//出列由于使用注册所以不需要判断if(!cell)

customTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

//行文本内容

cell.textLabel.text= [NSString stringWithFormat:@"%ld--%ld",indexPath.section,indexPath.row];

//行最右配件

cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

return cell;

}

//区头名称可不写

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

return @"区头";

}

//区头加载视图

-(UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {

//创建视图不要写添加return view;这一句就是默认添加

UIView * view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];

//写button

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(0, 0, 44, 44);

//绑定方法要传参

[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

//设置tag值

btn.tag = section + 10 ;

[view addSubview:btn];

//[btn release];不写否则运行时系统会崩溃

//设置图片视图

UIImageView * imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a"]];

imgView.frame = CGRectMake(0, 0, 44, 44);

[view addSubview:imgView];

[imgView release];

//设置图片视图的动画

if(flag[section] ==NO) {

imgView.transform = CGAffineTransformIdentity;

} else {

imgView.transform = CGAffineTransformMakeRotation(M_PI_2);

}

return view;

}

-(void)btnClick:(UIButton *)btn {

//检验测试用的输出可不写

NSLog(@"btnClick");

NSLog(@"%ld",btn.tag);

//点击取反按钮状态

flag[btn.tag- 10] = !flag[btn.tag- 10];

//检验测试用的输出可不写

NSLog(@"%ld",btn.tag);

//NSIndexSet:索引的集合其中的参数是想要刷新的区的集合

//用区创建一个集合集合的元素就是区号012

NSIndexSet * set = [NSIndexSet indexSetWithIndex:btn.tag- 10];

//行的动画

[table reloadSections:set withRowAnimation:UITableViewRowAnimationFade];

}

@end


UITableView的索引条索引

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

return index;

}

//索引标题方法返回的是索引标题数组

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

NSMutableArray * array = [NSMutableArray array];

for(int i = 0; i < 6; i ++) {

NSString * str = [NSString stringWithFormat:@"第%d区",i];

[array addObject:str];

}

return array;

}


UITableView的刷新

//刷新表格3种形式

//1.[table reloadData];全部刷新刷新整个表

//2.局部刷新Sections:要刷新的区索引的集合

NSIndexSet * set = [NSIndexSet indexSetWithIndex:btn.tag-100];

//局部刷新区(NSIndexSet *)sections要刷新的区索引的集合

[table reloadSections:set withRowAnimation:UITableViewRowAnimationFade];

//3.局部刷新行(row)

//    NSIndexPath * path = [NSIndexPath indexPathForRow:2 inSection:0];

//    NSArray * array = @[path];

//    [table reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];


注册cellUITableView

//注册创建cell

//1.注册系统的cell类

//[UILabel class];

//[UIView class];

//[table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

//2.注册自定义类的

//[table registerClass:[customTableViewCell class] forCellReuseIdentifier:@"cell"];

//3.注册Xib  cell

[tableregisterNib:[UINibnibWithNibName:@"XibTableViewCell"bundle:nil]forCellReuseIdentifier:@"cell"];

注册cell的话不需要判断

if(!cell){

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

}

UITableView

//UITableView:表格

UITableView * table = [[UITableViewalloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];

table.backgroundColor = [UIColor orangeColor];

//UITableView代理

table.delegate = self;

//数据源代理data  

table.dataSource = self;

[self.view addSubview:table];

#pragma mark ====数据源代理

//返回多少行Section区参数区里有多少行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if(section ==0) {

return 6;

} else {

//返回值方法返回多少行

return 10;

}

}

//返回cell每一行单元格

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//创建cell

//dequeue:队列

//返回cell的时候,先从重用队列中找带有重用标识的cell(可重用的cell)

UITableViewCell * cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell"];

//如果重用队列里没有那么就去创建一个返回cell

if(!cell1) {

cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell1"];

}

//indexPath二维坐标indexPath.section:区(没有设置的情况下,默认返回一个0区) indexPath.row:行

//默认44高度

//cell.textLabel.text = @"单元格";

cell1.textLabel.text= [NSString stringWithFormat:@"%d----%d",indexPath.section,indexPath.row];

returncell1;

}

//返回多少个区

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return2;

}

//TableView代理方法返回行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//设置各个区不等高

if(indexPath.section==0) {//0区

switch(indexPath.row) {

case0:

case1:

return100;

break;

case2:

case3:

return200;

break;

default:

return44;

break;

}

}else{//1区

return44;

}

}

//点击行触发的方法

-(void)tableView:(nonnullUITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

//indexPath索引路径

NSLog(@"点击cell----%d区%d行",indexPath.section,indexPath.row);

}

相关文章

网友评论

      本文标题:UITableView

      本文链接:https://www.haomeiwen.com/subject/qpzputtx.html