UITableView(基本设置和常用的协议方法)
来源:互联网 发布:mac改用户名 编辑:程序博客网 时间:2024/06/16 07:05
创建部分
初始化方法.设置不同文本
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
self.arr = [NSMutableArrayarrayWithObjects:@"宋江",@"卢俊义",@"吴用",@"公孙胜",@"关胜",@"林冲",@"秦明" ,@"呼延灼" ,@"花容",@"柴进",@"李应",@"朱仝",@"鲁智深",@"武松",nil];
}
return self;
}
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColoryellowColor];
self.navigationController.navigationBar.translucent = NO;
self.title =@"表格";
UITableView *tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height - 64)style:UITableViewStylePlain];( - 64是为了显示最后半个cell)
tableView .backgroundColor = [UIColorwhiteColor];
[self.viewaddSubview:tableView];
[tableViewrelease];
设置行高
tableView.rowHeight = 100;
UITableView的两套代理方法
设置第一套协议的代理人
dataSource
tableView.dataSource = self;
第二套协议
tableView.delegate = self;
给tableView添加头视图
self.imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"psu.jpeg"]];
self.imageView.frame =CGRectMake(0,0,self.view.frame.size.width,200);
self.tableView.tableHeaderView =self.imageView;
去掉分割线
self.directoryTableView.separatorStyle =NO;
右上角出现一个可以点击的按钮(进行编辑)
self.navigationItem.rightBarButtonItem =self.editButtonItem;
直接打开tableview的可编辑模式
[self.tableView setEditing:YES animated:YES];
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
协议方法
tableview第一个必须实现的协议方法,指定分区内有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arr.count; 让行数和数组元素个数保持一致
}
第二个协议方法,主要是用来显示数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static特点
1.只初始化一次
2.如果没有初始值,默认是0
3.直到程序结束,才会消失
当cell显示结束之后,会把cell统一的放到重用池中,等需要cell显示了,先从重用池中寻找,看有没有闲置的cell,如果有的话就用闲置的cell如果没有的话就重新创建
staticNSString *reuse =@"reuse";(给重用池设置重用的标志,根据这个标志可以找到对应的重用池)
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:reuse];(
tableView通过重用标志在重用池中寻找闲置的cell,如果有闲置的cell,cell会保存一个有效的cell对象地址,如果没有,cell里面则是nil,空
)if (cell ==nil) {
cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:reuse]autorelease];
}
cell里有默认的三个控件
cell.textLabel.text = @"类似标题";
cell.detailTextLabel.text = @"类似正文";
}
tabelView里有多少个section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
设置区数
return10;
}
设置标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
if (section %2 ==1) {
return@"天罡";
}else {
return@"地煞";
}
}
在右边设置索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
{
按照区名进行索引的
return@[@"0",@"1",@"2",@"3",@"4",@"5"];
}
显示第几区的第几个元素.第二套协议(cell的点击方法,一般用于跳转和传值)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
打印当前点击的人名是什么
NSLog(@"section: %ld row:%ld人名:%@", indexPath.section, indexPath.row,self.arr[indexPath.row]);
点击之后跳到下一页
SecondViewController *secVC = [[SecondViewControlleralloc]init];
[self.navigationControllerpushViewController:secVCanimated:YES];
[secVCrelease];
}
重写系统的编辑按钮点击触发的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[supersetEditing:editinganimated:animated];(继承父类的点击方法)
[self.tableViewsetEditing:editinganimated:YES];(这条语句可以控制点击编辑按钮之后的切换状态)
}
设置哪些行可以进行编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
奇数行可以编辑,偶数行不能编辑
if (indexPath.row % 2 == 0) {
return NO;
} else {
return YES;
}
默认是yes
returnYES;
}
设置样式(插入,删除)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleDelete;(一共三种:添加,删除,无)
}
删除数据
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
先删除数据源
[self.arrremoveObjectAtIndex:indexPath.row];
刷新(方式一)
[self.tableView reloadData];
通过tableview来删除上面的cell(方式二);
第一个参数:指定删除哪一个分区的哪个行,把他作为一个元素放在数组里
第二个参数:删除动画
[self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
方式一和方式二 任选一个
}
}
修改删除按钮的标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
随意移动cell的顺序
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
1.先获取到起始位置的数据
NSString *str = [self.arr[sourceIndexPath.row]retain];
2.把起始位置的对象从数据源中移除
[self.arrremoveObjectAtIndex:sourceIndexPath.row];
3.把数据插入到数组的目的位置上去
[self.arrinsertObject:stratIndex:destinationIndexPath.row];
[strrelease];
}
这个方法是ios8.0之后出现的方法,可以在编辑状态的时候有多个按钮
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
按钮的点击索要触发的事件,都是写在block中
NSLog(@"触发了删除按钮");
}];
deleteAction.backgroundColor = [UIColor cyanColor];
UITableViewRowAction *deleteAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"添加" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"触发了添加方法");
}];
return @[deleteAction, deleteAction1];
}
- UITableView(基本设置和常用的协议方法)
- UITableView和NavigationBar的常用设置
- UITableView的常用方法和属性
- UITableView的常用方法
- UITableView常用属性和方法
- UITableView的基本方法总结
- UITableView 常用的几个方法
- UITableView的常用代理方法
- UITableView的常用代理方法
- UITableView的一些常用方法
- UITableView的常用代理方法
- UITableView的常用代理方法
- UITableView 常用的委托方法:
- UITableView 设置标题的方法
- UIScrollView的基本属性和协议方法
- UITableView和UITableViewCell常用属性和方法
- UITableView的一些常用细节设置
- UITableView常用属性和方法基础
- hdu1075
- 第三周第二天
- JS验证身份证号码正确性
- Qt 串口类QSerialPort 使用笔记
- sunburnt 学习笔记 (一) sunburnt 简介和安装
- UITableView(基本设置和常用的协议方法)
- 使用DbUnit进行数据库操作的单元测试
- UI10_tableView的编辑
- BP神经网络对音频分类
- hdu5375(2015多校7)--Gray code(dp)
- AndroidImageSlider
- 从实例谈面向对象编程(OOP)、工厂模式和重构
- 3.1.5 类型转换;装箱和拆箱
- 通讯录系统(VS2010控制台程序)