iOS TableView 分组

 

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

@interface ZLViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>。函数

 

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

如:code

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

在函数viewDidLoad添加以下:orm

- (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; }
相关文章
相关标签/搜索