欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UIView 与 UITableView

程序员文章站 2022-07-15 16:42:35
...

 

UIView 

UIView 继承于 UIResponder类,UIView 用于实现视图。UIView类定义了视图的基本属性和方法  。

UIView 的类定义是一个显示在屏幕上的矩形区域,并在这方面管理内容的接口。

 

   initWithFrame:

   初始化并返回一个新的拥有特定大小的视图对象 

   UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];

   backgroundColor:

   背景的颜色

   V.backgroundColor=[UIColor brownColor];

   addSubview:

   加一个视图到一个视图里面

   [self.View addSubview:v];

   [v1 addSubview:v2];

   bringSubviewToFront:

    将子视图移到前面

    sendSubviewToBack:

    将子视图推送到后面:

    removeFromSuperview:

     把子视图删除

    insertSubview:atIndex:

    在特定位置插入子视图

    intsertSubview:aboveSuview:

   插入子视图在某个视图之上

   intsertSubview:belowSubview:

    插入子视图在某个视图之下

    exchangeSubviewAtIndex:withSubviewAtIndex:

    交换两个子视图的位置

UITableView

UITableView 的一个表视图是一种手段,用于显示和编辑信息的分层列表。UITableView 继承自UIScrollView(滚动视图),有两种风格 UITableViewStylePlain(不分组显示)和UITableViewStyleGrouped(分组显示)。UITableView 有两个协议 UITableViewDelegate(委托协议) 和UITableViewDataSource(数据源协议)。

 

UITableViewDataSource 是提供显示在表格单元上用的数据。其中有三个必须实现的核心方法:

  -(NSInteger)numberofSectionInTableView:(UITableView*)tableView;

  获得表视图的块个数,默认返回1

 

 -(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSItneger)section;

   这个方法返回指定块中的行数,不同的块返回不同的行数可以用switch和case语句

 

 -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)index;

 这个方法返回某一行的数据(表单元)NSIndexPath是块的编号(section)和行号(row)的组合。通过NSIndexPath

 就可以确定表单元(Table cell)位置。

 

UITableViewDelegate主要是提供一些可选的方法,用来控制tableview的选择  、指定section 的头和尾的显示以及协助完成cell的删除和排序的功能。一些委托方法:

 

 -(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView;

设置Section的数量

 

 -(NSSTring*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)setion;

设置每个section显示的Title

 

 -(NSInteger)tableView:(UITableView*)tableViewindentationLevelForRowAtIndexPath: (NSIndexPath*)indexPath;

设置让UITableView行缩进

 

NSIndexPath *de=[NSIndexPath indexPathForRow:row inSection:section];

[TopicsTable selectRowAtIndexPath:de animated:YES scrollPosition:UITableViewScrollPositionNone];

返回当前所选Cell

 

[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];设置 Table cell 有分割线

[tableView setBackgroundColor:UIColor clearColor] 设置UITableView背景为透明

 

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

设置选中Cell的响应事件

 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 选中后的反显颜色即刻消失

 

-(NSIndexPath*)tableView:(UITableView*)tableViewwillSelectRowAtIndexPath:(NSIndexPath*)indexPath;

设置选中的行所执行的动作