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
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
相关文章推荐
- IOS开发学习笔记028-UITableView单组数据显示代码优化
- 转发iOS UITableView学习笔记
- IOS学习笔记(十一)之IOS开发之表视图(UITableView)的基本介绍(一) 推荐
- 学习笔记一、UITableView上下滚动导航栏渐变隐藏和显示效果
- IOS学习笔记-UITableView (一)
- IOS学习笔记-UITableView表索引的颜色设置
- 方法定制iOS学习笔记8-UITableView的定制
- iOS学习笔记---数据库SQLite3的基本操作并讲内容显示在tableView中
- iOS学习笔记—— UItableView 控件的简单使用
- 黑马程序员-IOS学习笔记-UITableView、UITableViewCell、UITableViewController
- IOS UITableView 学习笔记
- iOS学习笔记13— UITableView封装之下拉-上提-图片异步加载
- [iOS]UINavigationController UITabBarController UITableView学习笔记
- IOS开发学习笔记026-UITableView的使用
- iOS学习笔记(4)— UITableView的重用机制
- ios学习临时笔记-UITableView学习
- IOS学习笔记(十二)之IOS开发之表视图(UITableView)的相关类,属性与表视图实现学习(二)
- iOS笔记:查询结果为空,显示无数据的方法(UITableView Display No Data)
- IOS学习:UITableView使用详解3 分组表的简单使用
- iOS Programming 学习笔记 - 03 UITableView和UITableViewController