• 模仿聊天窗口的分组的效果


    #import "AViewController.h"
    
    #define max 8888888888
    @interface AViewController ()<UITableViewDelegate,UITableViewDataSource>
    
    @property (nonatomic,strong)UITableView *myTabView;
    @property (nonatomic,strong)NSArray     *dataArray;
    @property (nonatomic,assign)NSInteger   rowNumber;
    @property (nonatomic,assign)NSInteger   sectionNumber;
    @end
    
    @implementation AViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
            self.rowNumber = 0;
            self.sectionNumber=max;
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        [self.view addSubview:self.myTabView];
        
    }
    
    -(UITableView *)myTabView
    {
        if (!_myTabView) {
            CGRect rect = self.view.bounds;
            rect.origin.y=20;
            rect.size.height=rect.size.height-rect.origin.y;
            _myTabView = [[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain];
            _myTabView.delegate = self;
            _myTabView.dataSource = self;
        }
        return _myTabView;
    }
    -(NSArray*)dataArray
    {
        if (!_dataArray) {
            self.dataArray = @[@{@"array": @[@"11",@"12",@"13",@"14",@"15"],@"name":@"one"},
                               @{@"array": @[@"21",@"22",@"23",@"24",@"25"],@"name":@"two"},
                               @{@"array": @[@"31",@"32",@"33",@"34",@"35"],@"name":@"three"},
                               @{@"array": @[@"41",@"42",@"43",@"44",@"45"],@"name":@"four"},
                               @{@"array": @[@"51",@"52",@"53",@"54",@"55"],@"name":@"five"}];
        }
        return _dataArray;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return self.dataArray.count;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    {
        if (_sectionNumber!=max) {
            if (section==_sectionNumber) {
                return 1+_rowNumber;
            }
            return 1;
        }else{
            return 1;
        }
    }
    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *indexCell = @"groupCell";
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:indexCell];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indexCell];
        }
        if (indexPath.row==0) {
            cell.textLabel.text = self. dataArray[indexPath.section][@"name"];
        }else{
            cell.textLabel.text = self.dataArray[indexPath.section][@"array"][indexPath.row-1];
        }
        return cell;
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSUInteger number =[self.dataArray[indexPath.section][@"array"]  count];
            if (indexPath.row==0) {
                if(_sectionNumber == max){
                    _sectionNumber =indexPath.section;
                    [self addCellwithNumber:number];
                }else if (_sectionNumber==indexPath.section) {
                    [self removeCellwithNumber:number];
                    _sectionNumber =max;
                }else{
                    [self removeCellwithNumber:number];
                    _sectionNumber = indexPath.section;
                    [self addCellwithNumber:number];
                }
                
            }
    }
    
    -(void)removeCellwithNumber:(NSInteger)number
    {
        for (int i=(int)number; i>=1; i--) {
            --_rowNumber;
            [self.myTabView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:_sectionNumber]] withRowAnimation:UITableViewRowAnimationTop];
        }
    
    }
    -(void)addCellwithNumber:(NSInteger)number
    {
        for (int i=1; i<=number; i++) {
            ++_rowNumber;
            [self.myTabView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:_sectionNumber]] withRowAnimation:UITableViewRowAnimationTop];
        }
    }
    @end

    .h文件里啥都木有,就不贴了。

    其实关于这个,没什么好说的,就是要注意折腾一个问题,在改变tableView的数据时的问题,数据变化一定要与试图对应,否则,各种蹦蹦蹦。

     另外还需要注意的代码帮助动画连贯

    具体的用法,就是在cell 被insert和delete的时候最好是加上,保证动画的流畅性

  • 相关阅读:
    Windowsforms 中对文件操作
    ADO.net增删改的使用
    ADO.net数据访问
    可空类型
    FineUI 页面跳转
    ASP.NET页面之间传递值的几种方式
    C# Find() 与 FindAll()方法的使用
    在Sql中将 varchar 值 '1,2,3,4,5,6' 转换成数据类型 int
    DataSet、DataTable、DataRow、DataColumn区别及使用实例
    C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串
  • 原文地址:https://www.cnblogs.com/lingzhiguiji/p/3919964.html
Copyright © 2020-2023  润新知