模仿聊天視窗的分組的效果(粗糙的Demo)

來源:互聯網
上載者:User

標籤:style   blog   color   io   strong   檔案   資料   for   

#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的資料時的問題,資料變化一定要與試圖對應,否則,各種蹦蹦蹦。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.