iOS TableView 分组

 

1.在类名为ZLViewController的.xib文件上面拖上控件tableview,tableview控件命名为命名为chatWindow。在ZLViewController类的头文件中添加协议

@interface ZLViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>。

 

2.在.m文件里面添加全局的两个动态数组变量,动态数组sections存放的是group(即组)的标题内容,动态数组rows存放的是cell(即行)的内容。

如:

@interface ZLViewController ()
{
    
    NSMutableArray *sections;
    NSMutableArray *rows;
  
}

在函数viewDidLoad添加如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

//为tableview添加委托和表视图的数据源
    self.chatWindow.delegate=self;
    self.chatWindow.dataSource=self;

 //初始化两个动态数组
    sections=[[NSMutableArray alloc]init];
    rows=[[NSMutableArray alloc]init];
    
//为两个动态数组添加对象
    [sections addObject:[NSString stringWithFormat:@""]];
    [sections addObject:[NSString stringWithFormat:@""]];
    [sections addObject:[NSString stringWithFormat:@""]];
    [sections addObject:[NSString stringWithFormat:@""]];
    
    for (int i=0; i<5; i++) {
        NSString *str=[NSString stringWithFormat:@"张 %d",i];
        [rows addObject:str];
    }
   
    for (int i=5; i<10; i++) {
        NSString *str=[NSString stringWithFormat:@"李 %d",i];
        [rows addObject:str];
    }
    for (int i=10; i<15; i++) {
        NSString *str=[NSString stringWithFormat:@"王 %d",i];
        [rows addObject:str];
    }
    for (int i=15; i<20; i++) {
        NSString *str=[NSString stringWithFormat:@"周 %d",i];
        [rows addObject:str];
    }
    
}

3.为Tableview添加相应的方法如下:

#pragma mark -
#pragma mark Table View DataSource Methods

//指定有多少个分区(Section),默认为1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [sections count];
}

//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [rows count]/[sections count];
}

//改变行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

//添加每个分区标题和脚本信息
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//动态数组里面的内容
return sections[section]; } - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @"显示"; } //每一行的内容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *TableSampleIdentifier = @"TableSampleIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier]; } NSUInteger row = [indexPath row];
//计算cell在动态数组rows中的位置(rows的值只是0,1,2,3,5.section的值只是0,1,2,3,每一个section对应rows的五个值(0,1,2,3,4)) NSUInteger rowResult
=row+(indexPath.section*5);

//设置每一行(即cell)的内容如下 cell.textLabel.text
=[rows objectAtIndex:rowResult]; return cell; } //行缩进 -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; return row; }

 

转载于:https://www.cnblogs.com/liukunyang/p/3361793.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值