首页 > 代码库 > 模仿聊天窗口的分组的效果(粗糙的Demo)
模仿聊天窗口的分组的效果(粗糙的Demo)
#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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。