【iOS开发-58】tableView初识:5个重要方法的使用和2种样式的差别
创建一个tableView,直接拖拽放在storyboard里面就可以。
(1)先创建一个数据模型类WSCarGroup,在WSCarGroup.h文件里:
#import <Foundation/Foundation.h> @interface WSCarGroup : NSObject @property(nonatomic,copy) NSString * title;
@property(nonatomic,copy) NSString * desc;
@property(nonatomic,strong) NSArray *cars; @end
(2)在ViewController.m中:
——先把数据转成模型,这里没有plist。所以直接手动输入。
先声明一个装模型的数组变量
@property (nonatomic,strong) NSArray *carGroups;
-(NSArray *)carGroups{
if (_carGroups==nil) {
WSCarGroup *cg1=[[WSCarGroup alloc]init];
cg1.title=@"德系汽车";
cg1.desc=@"质量最精良";
cg1.cars=@[@"宝马",@"奥迪",@"奔驰"]; WSCarGroup *cg2=[[WSCarGroup alloc]init];
cg2.title=@"日系汽车";
cg2.desc=@"非常轻非常省油";
cg2.cars=@[@"三菱",@"日产",@"本田",@"三菱",@"日产",@"本田",@"三菱",@"日产",@"本田",@"三菱",@"日产",@"本田"]; _carGroups=@[cg1,cg2];
}
return _carGroups;
}
——设置tableView的数据源,和协议相似。充当数据源的要遵守“协议”。此处我们把ViewController当做数据源。所以先遵守:
@interface ViewController ()<UITableViewDataSource>
——然后设置成数据源
- (void)viewDidLoad {
self.tableView.dataSource=self;
[super viewDidLoad];
}
——tableView最重要的几个方法
111、设置多少组(假设省略这种方法,则默认是1组)
222、设置每组有多少行
333、设置每行的cell填充什么内容(最重要)
444、设置每组头部标题文字
555、设置妹婿尾部描写叙述文字
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.carGroups.count;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
WSCarGroup *cg=self.carGroups[section];
return cg.cars.count;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//由于要返回UITableViewCell。所以先创建一个,然后赋值,最后返回,就可以。
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
WSCarGroup *cg=self.carGroups[indexPath.section];
cell.textLabel.text=cg.cars[indexPath.row];
return cell;
} -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
WSCarGroup *cg=self.carGroups[section];
return cg.title;
} -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
WSCarGroup *cg=self.carGroups[section];
return cg.desc;
}
——当然。为了展示方便,隐藏顶部状态栏
-(BOOL)prefersStatusBarHidden{
return YES;
}
tableView有两种展现形式:plain和grouped。
——plain就是每组之间间隔非常小,分组不明显。grouped每组间隔大分组明显。例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
——plain有头部悬停效果。相似于QQ分组好友的那个组名称在最顶部悬停,直到被下一组顶替。
最新文章
- (2)从实际项目谈起,基于MEF的插件框架之总体设计
- RDIFramework.NET — 基于.NET的快速信息化系统开发框架 — 系列目录
- 模拟java.util.Collection一些简单的用法
- form表单验证和事件、正则表达式
- MVC HTTP 错误 403.14 - Forbidden
- Amazon S3云存储服务器的功能及编程接口
- Bin Packing
- 将外卖O2O广告一棍子打成竞价排名,秤把平了吗?
- HTML中动态图片切换JQuery实现
- STL for_each()
- (转载)MySQL关键字GROUP BY的使用
- Objective-C学习篇02—封装
- Web开发在线工具
- 关于Storm Stream grouping
- Java课堂作业01
- flutter 入口文件配置路由+加载页面
- Google C++ 代码规范
- JAVA内部类小结
- 典型 python 小练习
- Algorithm 算法基础知识(未完成
热门文章
- [转] 在vue中使用SockJS实现webSocket通信
- 27、android log日志
- Leetcode30--->;Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置)
- 聊聊、Highcharts 动态数据优化版
- ASP.NET MVC下使用SWFUpload完成剪切头像功能
- beautifusouptest
- Welcome-to-Swift-10属性 (Properties)
- 论蛋疼的调戏matrix67的首页
- python -- DNS处理模块dnspython
- Linux 之 MySQL主从同步