• Sagit.Framework For IOS 自动布局教程:15、UITableView、UITableViewCell、UICollectionView、UICollectionViewCell特殊用法介绍。


    前言:

    框架对于UITableView、UITableViewCell 封装了不少功能,下面开始介绍。

    1、UITableView 基本定义。

    @interface UITableView(ST)
    
    #pragma mark 核心扩展
    typedef void(^AddTableCell)(UITableViewCell *cell,NSIndexPath *indexPath);
    typedef BOOL(^DelTableCell)(UITableViewCell *cell,NSIndexPath *indexPath);
    typedef void(^AfterTableReloadData)(UITableView *tableView);
    //!用于为Table追加每一行的Cell
    @property (nonatomic,copy) AddTableCell addCell;
    //!用于为Table移除行的Cell
    @property (nonatomic,copy) DelTableCell delCell;
    //!用于为Table reloadData 加载完数据后触发
    @property (nonatomic,copy) AfterTableReloadData afterReload;
    //!获取Table的数据源
    @property (nonatomic,strong) NSMutableArray<id> *source;
    //!设置Table的数据源
    -(UITableView*)source:(NSMutableArray<id> *)dataSource;
    //!存档所有Cell的高度(由系统控制)[存档格式为:section key,[row Array]]
    @property (readonly,nonatomic,retain) NSMutableDictionary *heightForCells;
    
    //!是否重用Cell(默认Yes)
    -(BOOL)reuseCell;
    -(BOOL)reuseCell:(BOOL)yesNo;
    //!是否自动控制Table的高度
    -(BOOL)autoHeight;
    //!设置是否自动控制Table的高度
    -(UITableView*)autoHeight:(BOOL)yesNo;
    //!获取默认的UITableViewCellStyle
    -(UITableViewCellStyle)cellStyle;
    //!设置默认的UITableViewCellStyle
    -(UITableView*)cellStyle:(UITableViewCellStyle)style;
    //!获取是否允许删除属性
    -(BOOL)allowDelete;
    //!设置是否允许删除
    -(UITableView*)allowDelete:(BOOL)yesNo;
    //!移除数据源和数据行
    -(UITableView*)afterDelCell:(NSIndexPath*)indexPath;
    #pragma mark 扩展属性
    -(UITableView*)scrollEnabled:(BOOL)yesNo;
    -(UITableView*)sectionCount:(NSInteger)count;
    //!每个Section的num数:参数可以传递:@[@"1",@"2",@"2",@"1"] 或者:@"1,2,2,1"
    -(UITableView*)rowCountInSections:(id)nums;
    @end

    2、UITableViewCell 基本定义:

    @interface UITableViewCell(ST)
    //!获取当前所在的table,(weak,不能造成双strong引用)
    @property (readonly,nonatomic,weak) UITableView *table;
    //!获取Cell的数据源
    @property (nonatomic,strong) id source;
    //!Cell是否重用的Cell,如果是,就不要再添加子控制,避免重复添加。
    //@property (readonly,nonatomic,assign) BOOL isReused;
    //!设置Cell的数据源
    -(UITableViewCell *)source:(id)dataSource;
    //!创建或复用Cell
    + (instancetype)reuseCell:(UITableView *)tableView index:(NSIndexPath *)index;
    //!获取Cell所在的行数
    -(NSIndexPath*)indexPath;
    -(UITableViewCell*)indexPath:(NSIndexPath*)indexPath;
    //!获取是否允许删除属性
    -(BOOL)allowDelete;
    //!设置是否允许删除
    -(UITableView*)allowDelete:(BOOL)yesNo;
    //!数据源中的第一个字段,系统自动设置
    -(NSString*)firstValue;
    -(UITableViewCell*)firstValue:(NSString*)value;
    //当Cell的高度在绑定后,需要动态根据子内容高度变化,再次刷新高度时使用。
    -(UITableViewCell*)resetHeightCache;
    #pragma mark 扩展属性
    -(UITableViewCell*)accessoryType:(UITableViewCellAccessoryType)type;
    -(UITableViewCell*)selectionStyle:(UITableViewCellSelectionStyle)style;
    
    @end

    UITableView 基本用法示例一:

    [[[[[sagit addTableView:nil] autoHeight:YES]  610] toCenter] block:^(UITableView* table)
         {
             [table relate:TopBottom v:60 v2:60];
             [table backgroundColor:ColorClear];
             table.backgroundView=[[[[UIImageView new] image:@"answer_bg_rank"]stretch] 1 height:1];
             table.separatorColor = [ColorWhite alpha:0.2];
             table.separatorInset=UIEdgeInsetsMake(0,26, 0, 26);
             table.addCell = ^(UITableViewCell *cell, NSIndexPath *indexPath)
             {
                 [cell 1 height:140]; // 1 代表 100% <=1 被处理成百分比。
                 cell.accessoryType=UITableViewCellAccessoryNone;
                 cell.backgroundColor=ColorClear;
                 
                 
                 //数据
                 AnswerUserRankModel *model=[[AnswerUserRankModel new] initWithObject:cell.source];
                 NSString *score=[[@(model.Score) stringValue] append:@""];
                 
                 [cell.contentView block:^(UIView* view)
                  {
                      [view 1 height:1];
                      if(indexPath.row!=table.source.count-1)
                      {
                          [[[[view addLine:nil color:@"#81F0D7"] 2 height:30]y:140-15] relate:Right v:26];
                          [[[[view addLine:nil color:@"#81F0D7"] 30 height:2] y:140] relate:Right v:26];
                      }
                      
                      [view addLabel:nil text:STNumString(model.Rank) font:30 color:ColorWhite];
                      [[STLastView x:50] toCenter:Y];
                      
                      [[[[view addImageView:nil] url:model.PhotoPath] 80 height:80] corner:YES];
                      [[STLastView x:122] toCenter:Y];
                    
                      [[[[view addLabel:nil text:model.NickName font:30 color:@"#FB8107"] onRight:STPreView x:42] toCenter:Y] 72*2];
                      [[[view addImageView:nil img:@"answer_bg_rankscore"] 196 height:20] block:^(UIImageView* scoreView)
                       {
                           [[scoreView x:192*2] toCenter:Y];
                           //[[scoreView relate:Right v:160] toCenter:Y];
                           [[scoreView addLabel:nil text:score font:30 color:@"#81F0D7"] toCenter];
                           //[scoreView stSizeToFit];
                       }];
                      [view onClick:^(id view)
                       {
                           [self stPush:[STNew(@"User") key:@"uid" value:model.UserID]];
                       }];
                  }];
                 
             };
         }];

    效果:

    UITableView 基本用法示例二:

     [[[[sagit addTableView:@"tableView" style:UITableViewStyleGrouped] autoHeight:YES] onBottom:STPreView y:20]  block:nil on:^(UITableView* table) {
            [table reuseCell:NO];
            table.addCell = ^(UITableViewCell *cell, NSIndexPath *indexPath)
            {
                NSString *name=cell.source[@"name"];
                NSString *title=cell.source[@"title"];
                NSString *holder=cell.source[@"holder"];
                if(holder==nil){holder=title;}
                NSString *pick=cell.source[@"pick"];
                NSString *maxLength=cell.source[@"maxLength"];
                
                [[[cell.contentView addLabel:nil text:title font:30] relate:Left v:50] toCenter:Y];//label是固定的。
                UIView *textView;
                
                if([title isEqualToString:@"简介"])
                {
                    textView=[[[[[cell.contentView addTextView:name placeholder:holder font:30] maxRow:3] maxLength:100]
                              textAlignment:NSTextAlignmentLeft] height:0.8];
                    UIEdgeInsets inset=textView.asTextView.textContainerInset;
                    inset.left-=5;
                    textView.asTextView.textContainerInset=inset;
                }
                else
                {
                    textView=[[cell.contentView addTextField:name placeholder:holder font:30] height:0.8];
                    if(maxLength!=nil)
                    {
                        [STLastTextField maxLength:[maxLength integerValue]];
                    }
                }
                [[[textView onRight:STPreView x:50] relate:Right v:0] toCenter:Y];
                
                if(pick==nil)
                {
                    cell.accessoryType=UITableViewCellAccessoryNone;
                }
                else
                {
                    [[STLastView addClick:@"pick"] key:@"pick" value:pick];
                }
            };
            table.afterReload = ^(UITableView *tableView)
            {
                if(!self.hasLoaded)
                {
                    self.hasLoaded=YES;
                    [self setToAll:Sagit.Global.User.user];
                }
            };
            
        }];

    效果:

    数据源设置:

    -(void)initData
    {
        [super initData];//加载子UI的数据。
        UITableView *table=STFirstTable;
        table.source=@[@{@"title":@"姓名",@"name":@"NickName",@"maxLength":@"15"},
                       @{@"title":@"简介",@"name":@"Description",@"holder":@"请简单介绍一下自己!"},
                       @{@"title":@"公司",@"name":@"Company",@"holder":@"所在公司!",@"maxLength":@"20"},
                       @{@"title":@"年龄",@"name":@"Age",@"pick":@"年龄"},
                       @{@"title":@"学历",@"name":@"Edu",@"pick":@"学历"},
                       @{@"title":@"身高",@"name":@"Height",@"pick":@"身高"},
                       @{@"title":@"星座",@"name":@"Constellation",@"pick":@"星座"},
                       @{@"title":@"婚姻",@"name":@"MarrStatus",@"pick":@"婚姻"},
                       @{@"title":@"职业",@"name":@"Profession",@"pick":@"职业"},
                       @{@"title":@"收入",@"name":@"Wages",@"pick":@"月薪"}];
        
        [[table sectionCount:2] rowCountInSections:@"3,7"];
        //table.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,1, 0.01f)];
        table.sectionFooterHeight=0.2;
        // table.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,1, 1.0f)];
        //self.automaticallyAdjustsScrollViewInsets = NO;
        [table reloadData];
        
    }

    UICollectionView、UICollectionViewCell 基本定义

    @interface UICollectionView(ST)
    #pragma mark 核心扩展
    typedef void(^AddCollectionCell)(UICollectionViewCell *cell,NSIndexPath *indexPath);
    typedef BOOL(^DelCollectionCell)(UICollectionViewCell *cell,NSIndexPath *indexPath);
    //!用于为Table追加每一行的Cell
    @property (nonatomic,copy) AddCollectionCell addCell;
    //!用于为Table移除行的Cell
    @property (nonatomic,copy) DelCollectionCell delCell;
    //!获取Table的数据源
    @property (nonatomic,strong) NSMutableArray<id> *source;
    //!设置Table的数据源
    -(UITableView*)source:(NSMutableArray<id> *)dataSource;
    @end

    ---------------------------------------------

    @interface UICollectionViewCell(ST)
    //!获取Cell的数据源
    @property (nonatomic,strong) NSMutableDictionary<NSString*,id> *source;
    //!设置Cell的数据源
    -(UICollectionViewCell *)source:(NSMutableDictionary<NSString*,id> *)dataSource;
    //!创建或复用Cell
    + (instancetype)reuseCell:(UICollectionView *)tableView index:(NSIndexPath *)index;
    //!获取当前所在的table
    -(UICollectionView*)table;
    
    //!获取是否允许删除属性
    //-(BOOL)allowDelete;
    //!设置是否允许删除
    //-(UITableView*)allowDelete:(BOOL)yesNo;
    //!数据源中的第一个字段,系统自动设置
    -(NSString*)firstValue;
    -(UICollectionViewCell*)firstValue:(NSString*)value;
    @end

    这两个和上面两个的用法基本一致,功能比上面的还少,就不过多介绍了。

  • 相关阅读:
    CDN缓存
    nginx作用
    Linux下4个查找命令which、whereis、locate、find
    @ModelAttribute的用法,与@RequestBody的区别
    将kafka消息解析拷贝
    永久代溢出(java.lang.OutOfMemoryError: PermGen space )
    失败重试机制-递归应用1
    kafka-重复消费-2
    读写分离-延时问题-1
    UILabel处理html标签
  • 原文地址:https://www.cnblogs.com/cyq1162/p/13509337.html
Copyright © 2020-2023  润新知