zoukankan      html  css  js  c++  java
  • UITableView

    UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped

     <UITableViewDataSource,UITableViewDelegate>协议 

    ##UITAbleeViewDataSource协议

    //调用数据源的下面方法得知一共有多少组数据

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

     //调用数据源的下面方法得知每一组有多少行数据

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

     //调用数据源的下面方法得知每一行显示什么内容

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

    ###Cell的重用代码
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 1.定义一个cell的标识
        static NSString *ID = @”cell";
        // 2.从缓存池中取出cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        // 3.如果缓存池中没有cell
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
       
        // 4.设置cell的属性...
        //cell.textLabel.text = str;
        //cell.detailTextLabel.text = @"";
        //cell.accessoryType = UITableViewCellAccessoryCheckmark;
        //cell.accessoryView //设置右边视图
        //cell.backgroundView = view;系统默认,frame无法修改
        
        return cell;
    }
    
    ==================================
    //注册cell 可替代上面代码
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell”];
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
       //设置cell属性
        ....
        return cell;
    }
    Cell的重用代码

    // 4.设置cell的属性...
    //cell.textLabel.text = str;
    //cell.detailTextLabel.text = @"";
    //cell.accessoryType = UITableViewCellAccessoryCheckmark;
    //cell.accessoryView //设置右边视图
    //cell.backgroundView = view;系统默认,frame无法修改

     

    //设置cell的高度,cell默认高度44

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

    #pragma mark - 设置组头

    //设置段的标题

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

    //设置组头高度

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

    //设置组头的view

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

    #pragma mark - 索引

    //返回索引数组

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

    表格的编辑模式

    开启表格的编辑状态

     _tableView.editing = YES; 或

     - (void)setEditing:(BOOL)editing animated:(BOOL)animated; 开启表格编辑状态  

    //返回表格编辑编辑样式。不实现默认都是删除

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

    return editingStyle : UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert

    多选状态时,return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert   与选择和反选结合使用

    //根据editingStyle处理是删除还是添加操作完成删除、插入操作刷新表格

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

    #pragma mark - 删除和插入
    
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewCellEditingStyleInsert;
    }
    
    //代理方法, cell向左滑动出现delete
    //  点击delete执行代码
    //参数1: 编辑风格 Delete,Insert
    //参数2: 提供删除的行的位置
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"row = %ld",indexPath.row);
        if(editingStyle == UITableViewCellEditingStyleDelete
           )
        {
            NSMutableArray *subArray = _dataArray[indexPath.section];
            [subArray removeObjectAtIndex:indexPath.row];
        }
        if(editingStyle == UITableViewCellEditingStyleInsert)
        {
            NSString *insertString = @"新数据";
            NSMutableArray *subArray = _dataArray[indexPath.section];
            [subArray insertObject:insertString atIndex:indexPath.row+1];
        }
        
        [tableView reloadData];
    }
    
    //设置删除按钮
    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return @"删除";
    }
    删除和插入代码

    #pragma mark - 移动

    //注意: 一旦添加这个方法,编辑状态下每个cell出现移动标志

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

    #pragma mark - 移动
    //代理方法, 支持移动
    //参数1: 源位置
    //参数2: 目标位置
    //注意: 一旦添加这个方法,编辑状态下每个cell出现移动标志
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {
        
        //先从源数组拿出数据
        NSMutableArray *sourceArray = _dataArray[sourceIndexPath.section];
        NSString *string = sourceArray[sourceIndexPath.row];
        
        [sourceArray removeObjectAtIndex:sourceIndexPath.row];
        
        //插入目标数组
        NSMutableArray *destArray = _dataArray[destinationIndexPath.section];
        [destArray insertObject:string atIndex:destinationIndexPath.row];
        
    }
    View Code

    #pragma mark - 选择和反选

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

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

    #pragma mark - 选择和反选
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"sec=%ld row=%ld",indexPath.section,indexPath.row);
    }
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"des  sec=%ld row=%ld",indexPath.section,indexPath.row);
    }
    View Code

     

    自定义表格行UITableViewCell

     

    UISearchBar中有tableview,使用时应注意判断

    //让表格显示最后一行

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArray.count-1 inSection:0];

        [_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

  • 相关阅读:
    oc结构
    iOS分类
    iOS协议
    缓存无底洞现象
    数据库备份,恢复
    PHP邮件发送库:Swiftmailer
    PHP分页组件:Paginator
    PHP验证码类
    PHP日期和时间处理组件-Carbon
    composer的一些操作
  • 原文地址:https://www.cnblogs.com/caolongs/p/4766979.html
Copyright © 2011-2022 走看看