Imitating the grouping effect of the chat window (rough demo)

Source: Internet
Author: User
#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

In the. h file, I will not post anything.

In fact, there is nothing to say about this. We need to pay attention to a problem. When we change the tableview data, the data changes must correspond to the attempt. Otherwise, there will be various jumps.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.