美文网首页
TableView基础

TableView基础

作者: 杰boy | 来源:发表于2018-03-23 17:18 被阅读0次

    总结一些UITableView常见的问题 和 常用的方法
    iOS UITableView的多选
    UITableViewStyleGrouped类型的UITabelView使用技巧

    UITableView基础介绍

    • UITableView有两种样式:UITableViewStylePlain 和 UITableViewStyleGrouped
    • UItableViewCell有控件有四中样式下面是枚举类型 :
    typedef NS_ENUM(NSInteger, UITableViewCellStyle) { 
        UITableViewCellStyleDefault,    // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边) 
        UITableViewCellStyleValue1,        // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边) 
        UITableViewCellStyleValue2,        // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边) 
        UITableViewCellStyleSubtitle    // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边) 
    }; 
    

    总结一些UITableView的刷新方法

    • -(void)reloadData;刷新整个表格。
    • -(void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);刷新指定的分组和行。
    • -(void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);刷新指定的分组。
    • -(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;删除时刷新指定的行数据。
    • -(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;添加时刷新指定的行数据。

    UItableView的多选功能

    UItableViewCellEditingStyle有三种模式(单元格的编辑样式)
    • UITableViewCellEditingStyleDelete
    • UITableViewCellEditingStyleInsert
    • UITableViewCellEditingStyleNone
      多选设置同时包含 UITableViewCellEditingStyleDelete 和 UITableViewCellEditingStyleInsert
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    }
    
    cell点击效果是蓝色可以在自定义cell中设置效果
    self.multipleSelectionBackgroundView = [UIView new];
    
    cell也可以修改选中图标的颜色,其他默认图片颜色也会因此修改
    self.tintColor = [UIColor redColor];
    


    刷新UITableView的几个动画

    typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
                               UITableViewRowAnimationFade,   //淡入淡出
                               UITableViewRowAnimationRight,  //从右滑入         // slide in from right (or out to right)
                               UITableViewRowAnimationLeft,   //从左滑入
                               UITableViewRowAnimationTop,     //从上滑入
                               UITableViewRowAnimationBottom,  //从下滑入
                               UITableViewRowAnimationNone,            // available in iOS 3.0
                               UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy
                               UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you
    };
    


    单元格移动

    /**
     *  设置单元格移动样式
     */
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }
    /**
     *  sourceIndexPath:表示将要移动的单元格所处的位置
     *  destinationIndexPath:表示移动到哪个位置
     */
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
        //获取需要删除的元素数组
        NSMutableArray * arrayDelete = [_arrayDS objectAtIndex:sourceIndexPath.section];
        //保存删除的元素内容
        NSString * str = [arrayDelete objectAtIndex:sourceIndexPath.row];
        //从数组中删除该元素
        [arrayDelete removeObjectAtIndex:sourceIndexPath.row];
        //获取添加元素所在的数组
        NSMutableArray * arrayInsert = [_arrayDS objectAtIndex:destinationIndexPath.section];
        [arrayInsert insertObject:str atIndex:destinationIndexPath.row];
    }
    

    相关文章

      网友评论

          本文标题:TableView基础

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