zoukankan      html  css  js  c++  java
  • UITableView汇总

    1.声明数据源数组,声明全局的TableView

       NSMutableArray *_dataArray;

       UITableView *_tableView;

    2.实例化_tableView 遵守协议<UITableViewDeleate,UITableViewDataSoure>

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,460)];

    _tableView.delegate = self;

    _tableView.dataSoure = self;

    [self.view addSubView:_tableView];

    3.TableView的代理函数

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {//返回每行行高
        return 0;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {//返回每个分区的头标题行高
        return 100;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {//返回每个分区的脚标题行高
        return 50;
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {//返回分区数
        return 10;
    }

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

    {//返回每个分区包含多少多少行Cell

      return 100;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {//返回cell的样式和展示内容

       NSString *identifier = @"ID";//复用标识
       UITableViewCell *cell =[tableView dequdequeueReusableCellWithIdentifier:identifier];

       if(!cell){

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

    }

      //设置cell的展示内容 系统方法的cell包含textLabel、detailTextLabel

       cell.textLabel.text = _dataArray[indexPath.section] ;

       cell.detailTextLabel.text = @"";

        return cell;

    }

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {//自定义头标题的view
      
      UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
        //或者UIview *v =[headerView autorelease];
        headerView.backgroundColor = [UIColor greenColor];
    //    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    //    label.backgroundColor = [UIColor yellowColor];
    //    label.text = @"杨大傻";
    //    return (UIView *)label;
        return [headerView autorelease];//内存泄露 使用自动release
    }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {//选中表格视图某一行后,触发的方法

    }

    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {//cell附属的button 必须有蓝色的按钮出现,才能点击执行这个方法,点出附属按钮
    }

    - (void)customEditMethod
    {//自定义按钮实现编辑方法时,需要设置一个bool型成员变量
        _isEditing = !_isEditing;
        [_tableView setEditing:_isEditing animated:YES];
    }

    //系统自带的编辑按钮 作用:切换编辑状态和非编辑状态
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    //系统自带的编辑按钮时必须按照下面的方式重写编辑函数,固定写法
    -(void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:animated];
        [myTableView setEditing:editing animated:animated];
    }


    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {//编辑的方法,如果其它的编辑都不设,只设了这一个,那么就会有cell右滑出现删除的按钮,也是比较常用的
        if (editingStyle == UITableViewCellEditingStyleDelete){
            //删除
            [[dataArr objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];  //删除数据源记录
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//删除cell
        } else if (editingStyle == UITableViewCellEditingStyleInsert) {
            //插入
            [[dataArr objectAtIndex:indexPath.section] insertObject:@"a new cell" atIndex:indexPath.row];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }

    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {//删除按钮的字
        return @"删";
    }


    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {//返回编辑模式,默认是删除
        if (indexPath.section) {
            return UITableViewCellEditingStyleInsert;
        }
        return UITableViewCellEditingStyleDelete;
    }

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {//移动,不实现这个代理方法,就不会出现移动的按钮
        NSString *text=[[[dataArr objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row] copy];
        [[dataArr objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
        [[dataArr objectAtIndex:destinationIndexPath.section] insertObject:text atIndex:destinationIndexPath.row];
        [text release];
    }
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {//移动事件
        //通过数据源里找到需要移动的数据
        Student *stu = [[[_dataArr objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row] retain];//retain为插入保留引用计数
        //从原位置删除
        [[_dataArr objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
        //插入到新位置
        [[_dataArr objectAtIndex:destinationIndexPath.section] insertObject:stu atIndex:destinationIndexPath.row] ;
        [stu release];//retain加的1 给release了
    }


    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {//默认所有的都是可以编辑的(增,删)
        return YES;
    }

    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {//默认所有的都是可以移动的
        return YES;
    }

    4.在创建cell时也可以自定义cell的样式,先创建好cell的模型,在继承自UITableViewCell 的类中,用Xib创建好Cell的界面 将cell所要显示的控件声明成成员变量,便于接受外界传来的数据。引入cell的模型头文件后,在创建Cell的代理函数中,这样创建:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {//返回cell的样式和展示内容

        MyTableViewCell *cell = [tableView dequdequdequeueReusableCellWithIdentifier:@"ID"];
            if (!cell) {
                cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"]autorelease];
            }
        //找到对应的数据模型
        BookModel *bm = [_dataArr objectAtIndex:indexPath.row];
        [cell sendDataModel:bm];
        return cell;
        }
    5.cell的多选模式 在返回编辑模式的函数中同时返回 插入|删除

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    {//进入可多选模式

      return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;

    }

    6.关闭自动适应

    self.automaticallyAdjustsScrollViewInsets= YES;

    更加常用的是在设置了ScrollView、TableView、CollectionView时,在viewDidLoad中直接加上适配:

    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
            self.automaticallyAdjustsScrollViewInsets = NO;
            //适配  判断self是否响应setAutomaticallyAdjustsScrollViewInsets: 函数 如果响应 关闭自动适应
        }

    7.TableView的索引

    在实例化TableView的时候就可以设置索引条属性

        _myTableView.tableHeaderView.backgroundColor = [UIColor blueColor];
        //索引条背景色
        _myTableView.sectionIndexBackgroundColor = [UIColor clearColor];
        //索引条文字颜色
        _myTableView.sectionIndexColor = [UIColor greenColor];


    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {//系统方法
        //UITableViewIndexSearch 系统内置的字符串,会显示成搜索小图标
        NSMutableArray *arr = [[NSMutableArray arrayWithObject:UITableViewIndexSearch];
        //NSMutableArray (存放索引标题的数组)
        return arr;
    }


    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
    {//因为索引的前面加了个搜索小图标,所以需要重写这个方法点击的时候要返回的数要-1
        return index-1;
    }

  • 相关阅读:
    常用Linux命令的基本使用(不定时补充)
    利用油猴插件实现全网VIP视频免费看
    电脑开机显示被调用的对象已与其他客户端断开连接
    常见DOS命令
    Text函数对转换日期,调整数值,条件判断的应用技巧
    Excel常用函数
    Power BI(天猫双十一美妆销售数据分析)
    Power BI新建列与关系函数(三)
    Python_Bug
    Docker 初窥(有一大坑,注意根目录磁盘爆满问题,易导致开机黑屏)
  • 原文地址:https://www.cnblogs.com/zdong/p/4388445.html
Copyright © 2011-2022 走看看