zoukankan      html  css  js  c++  java
  • iOS tableView全面详解

    1.tableView有两种展现形式,UITableViewStylePlain和UITableViewStyleGrouped

    创建tableView

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT-44) style:UITableViewStylePlain];

    2.设置tableView的代理,必须实现的两个方法设置cell的内容和cell的高度,否则会出现崩溃的现象。

        self.table.dataSource = self;

        self.table.delegate = self;

    3.如果有尾部视图或者头部视图,还要设置self.tableView.tableHeaderView和self.tableView.tableFooterView

     //显示有多少组

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

    //显示每组的头部

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

    //显示每组的尾部

    -(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

    //显示每组的尾部标题有多高

    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

          return CGFLOAT_MIN;

    }

    //显示每组的头部标题有多高

    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

    {

          return CGFLOAT_MIN;

    }

    //显示每组头标题名称

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

    //显示每组尾标题名称

    -(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

    //显示每组有多少行

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

    //显示每组的行高

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

    //显示内容

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

     //打开或关闭编辑功能

        if ([self.table isEditing]) {

            [self.table setEditing:NO animated:YES];

        }else{

            [self.table setEditing:YES animated:YES];

        }

    //对应的行是否可以被编辑

    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 

    //设置编辑风格

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

        UITableViewCellEditingStyleNone,

        UITableViewCellEditingStyleDelete,删除

        UITableViewCellEditingStyleInsert添加

    }

    //提交编辑(左滑删除和添加)

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

        

        if (editingStyle == UITableViewCellEditingStyleDelete) {//删除

            [self.serviceArry removeObjectAtIndex:indexPath.row];

            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];//删除刷新

        }

        else if (editingStyle == UITableViewCellEditingStyleInsert) {//添加

           [datas insertObject:@"ab" atIndex:indexPath.row];

        [self.table insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];//添加刷新

     

        }

    }

    //设置是否可以移动

    -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

    //提交移动结果

    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

        //先把要移动的数据保存

        id moveObj = [datas objectAtIndex:sourceIndexPath.row];

        //从数组中删除要移动的数据

        [datas removeObjectAtIndex:sourceIndexPath.row];

        //把要移动的数据插入到目标位置

        [datas insertObject:moveObj atIndex:destinationIndexPath.row];

    }

         

    //每行的点击事件

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

  • 相关阅读:
    安装Php时候报错信息:virtual memory exhausted: Cannot allocate memory (不能分配内存)
    putty保持连接不自动段开
    利用iptables将本地的80端口请求转发到8080,当前主机ip为192.168.1.1,命令怎么写?
    linux上大量tcp端口处于TIME_WAIT的问题
    cacti出现snmp error
    洛谷3672:小清新签到题——题解
    BZOJ3040:最短路——题解
    洛谷4230:连环病原体——题解
    洛谷3934:Nephren Ruq Insania——题解
    洛谷3932:浮游大陆的68号岛——题解
  • 原文地址:https://www.cnblogs.com/hongyan1314/p/5790166.html
Copyright © 2011-2022 走看看