您的位置:首页 > 产品设计 > UI/UE

ios学习笔记之-UITableView的分组显示

2017-03-02 15:44 477 查看
UITableView在开发中用处非常广泛,汽车之家的品牌选择就是这个控件,还有我们iPhone设置页面的那些选项也是这个。所以这个控件一定要很好的掌握,并熟练的使用。UITableView继承自UIScrollView也就是说他是可以滚动的,他有两个模式一个是分组展示 一个是不分组,目前我看来基本上分组展示用的多,所以就介绍分组展示的用法。

UITableView需要数据源(dataSource)来显示数据

UITableView会向数据源查询总共多少组 每组多少行 每行数据有什么内容等等

凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源 

1.首先创建工程

2.h文件

#import <UIKit/UIKit.h>

@interface ViewController :
UIViewController

@property(nonatomic,retain)
UITableView*tableView;

@end
3.m 文件

//

//  ViewController.m

//  UITableView展示多组数据

//

//  Created by LiuJunHong on 17/3/2.

//  Copyright © 2017年 liujunhong. All rights reserved.

//

#import "ViewController.h"

@interface
ViewController ()<UITableViewDataSource>

@end

@implementation ViewController

@synthesize tableView=_tableView;

- (void)viewDidLoad {

    [super
viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.tableView = [[UITableView
alloc]initWithFrame:self.view.frame
style:UITableViewStyleGrouped];

    self.tableView.dataSource=self;//设置数据源

    [self.view
addSubview:self.tableView];

    

}

//返回显示多少组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return
3;

}

//每一组有多少条数据

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section==0) {

        return
3;

    }else
if(section==1){

        return
2;

    }else{

    

        return
1;

    }

}

//每组的每行显示什么

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{

 UITableViewCell *cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil

                          ];

    if (indexPath.section==0) {

        //第一组

        if (indexPath.row==0) {

        //第一行

            cell.textLabel.text=@"林平之";

        }else
if(indexPath.row==1){

        cell.textLabel.text=@"东方不败";

        }

            

        else{

            cell.textLabel.text=@"岳不群";

        

        }

    }else
if (indexPath.section==1) {

        //第2组

        if (indexPath.row==0) {

            //第一行

            cell.textLabel.text=@"张飞";

        }

        

        else{

            cell.textLabel.text=@"关羽";

            

        }

    }else{

    cell.textLabel.text=@"孙悟空";

        

        

    }

return cell;

}

//设置每一组的组标题

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if(section==0){

    return
@"笑傲江湖";

    }else
if(section==1){

    return
@"三国演义";

    }else{

        return
@"西游记";

    }

}

//设置每一组的组尾(简介)

-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    if(section==0){

        return
@"这个电视剧的令狐冲是李亚鹏";

    }else
if(section==1){

        return
@"这个电视剧的关羽是二爷";

    }else{

        return
@"这个电视剧的白骨精为什么不吃了傻逼唐僧";

    }

}

- (void)didReceiveMemoryWarning {

    [super
didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息