ioS开发之UI基础--UITableView简单了解(个人笔记)

来源:互联网 发布:男生休闲鞋推荐 知乎 编辑:程序博客网 时间:2024/04/29 15:12

1.UITableView默认只是一个空壳子

2.TabeleView不知到内部需要显示什么样内容,如何才能告诉tableView中显示内容呢?

3.它需要一个专门为它提供数据的代理-数据源


设置数据源三个步骤:

1.遵守数据源协议

2.设置设置控制器为tableView数据源

3.实现数据源方法

三个核心方法

//1.tableView中一共有多少组

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView

//2.tableView 每一组多少行

// section对应这个组有多少行

// 注意:section0开始

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

//3.每一行展示什么样内容

// indexPath.section 第几组

// indexPath.row     第几行


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



//4.设置组标题

// 用来告诉tableViewsection组的头部应该显示什么样的内容

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


//5.Footer 脚部,尾部,底部

//用来告诉tableViewsection组的底部显示什么样内容

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

/*

 1.使用字典不好

 1.取出数据时候,都是通过Key来取值,key一般都是字符串,字符串就容易写错

 2.写错误编译时候时候也不会报错

 3.没有提示,降低编码速度。

 

 2.使用模型

 1.模型:一种专门用来存储数据的对象

 2.好处:1.代码提示 2.写错就编译就报错了 3.比较专业,能显示你编程水平。

 

 

 字典转模型

 

 1.定义模型类

 2.声明模型属性,字典中一个Key对应于一个属性,

 key是什么名字那么属性也是什么名字

 字典中key对应值是什么样数据类型,那么模型中对应的属性也是什么数据类型

 

 3.ViewController中懒加载carGroups数据

 4.storyboard中中拖入一个tableView,然后连线到ViewController

 5.viewConroller成为tableView数据源

 1.创建项目,导入资源

 2.字典转模型

 2.1 创建一个模型类

 2.2 实现字典转模型的一个对象方法和一个类方法

 2.3 plist中加载数据,并且把字典数组转换为模型数据

 3. viewController中懒加载数据

 4. storybaord中拖入一个tableView,然后把这个tableView连线到viewController

 5. 设置tableView的数据源为viewController

 1.遵守数据源的协议

 2.设置控制器为tableView数据源

 3.实现数据源方法

 

 

 */

cell的常见属性

//   1.验证contentViewimageView,textLabel,detailTestLabel父控件

//   打印地址

//    cell中的子控件都是放到contentView

//    NSLog(@"%p",cell.contentView);

//    NSLog(@"%p",cell.imageView.superview);

//    NSLog(@"%p",cell.textLabel.superview);

//    NSLog(@"%p",cell.detailTextLabel.superview);

//  2.辅助视图的类型,学会如何给属性设置数据

//  2.1 按着command键点进去,看它是什么类型

//  2.2 如果是枚举看这个枚举类型中有什么值,一一实验即可

cell.accessoryType = UITableViewCellAccessoryCheckmark;

//  3 自定辅助视图,当自定义辅助视图后,accessoryType就失效

cell.accessoryView = [[UISwitch alloc] init];


//  4.设置cell的背景颜色

cell.backgroundColor = [UIColor brownColor];

//  5.自定背景视图,当有背景视图的时候,那么backgroundColor失效了

//    UIView *backgroundView = [[UIView alloc] init];

//    backgroundView.backgroundColor = [UIColor redColor];

//    cell.backgroundView = backgroundView;

//  6.设置选中状态下背景视图

UIView *backgroundView = [[UIView alloc] init];

backgroundView.backgroundColor = [UIColor greenColor];


cell.selectedBackgroundView = backgroundView;

tableView的常见属性


// 设置tableView的数据源为控制器

self.tableView.dataSource =self;


// 设置tableView行高

self.tableView.rowHeight =100;


//  设置tableView的分割线的类型

//  separator 分割线

// 表示没有分割线

self.tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;

// 单行分割线,默认分割线样式

self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

// cell没有沾满屏幕时候,空白的cell显示分割线

//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;(几乎不用)


//  设置分割线颜色

self.tableView.separatorColor = [UIColor redColor];

//  这里top,bottom 无论你设置为什么都是无效

self.tableView.separatorInset = UIEdgeInsetsMake(0,100,0,100);


//  设置tableView的背景颜色

self.tableView.backgroundColor = [UIColor blueColor];


//  设置tableView的背景视图

UIImageView *imageView = [[UIImageView alloc] init];

imageView.image = [UIImage imageNamed:@"04"];

self.tableView.backgroundView = imageView;


tableView的代理

使用场景:当需要监听tableView中的cell的选中或取消选中的时候,可以设置tableView的代理

步骤:

1.遵守UITableDelegate协议

2.让控制器成为tableView的代理

3.实现需要监听代理方法


//当用户选中某一行的时候,会调用的代理方法

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

//当用户取消选中某一个行的时候调用

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

//该方法可以为不同行指定不同的行高

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

/

0 0
原创粉丝点击