你的位置:首页 > 软件开发 > 操作系统 > iOS UI控件7(UITableView)

iOS UI控件7(UITableView)

发布时间:2016-05-30 09:00:13
1.表格(UITableView)与表格控制器(UITableViewController)UITableView是iOS开发中常见的UI控件,本质是一个列表(单列的表格)。UITableView允许自由控制行的控件,包括在表格行中添加多个字控件。UITableView继承了UI ...

iOS UI控件7(UITableView)

1.表格(UITableView)与表格控制器(UITableViewController)

UITableView是iOS开发中常见的UI控件,本质是一个列表(单列的表格)。UITableView允许自由控制行的控件,包括在表格行中添加多个字控件。UITableView继承了UIScrollView,具有UIScrollView的功能,这个UIScrollView主要封装了UITableViewCell单元格控件,因此UITableView默认可以对单元格进行滚动。默认情况下,所有UITableViewController实例被自动设为UIScrollView委托。

UITableView支持设置的属性:

  1. Style 指定表格的风格
    • Plain 普通风格
    • Grouped 分组风格
  2. Separator 指定表格行之间分割条的样式
    • 分隔条样式: 可以选择Single Line (单线) 和Single Line Etched
    • 分隔条颜色:
  3. Selection 控制表格的选择风格
    • No Selection
    • Single Selection
    • Multiple Selection
  4. Editing 控制表格处于编辑状态是否允许选择,
    • No Selection During Editing: 编辑状态不允许选中
    • Single Selection During Editing: 编辑状态只允许单选
    • Multiple Selection During Editing: 允许多选

可以通过下面的属性或方法来设置表格控件的外观:

  1. style 只读属性,返回表格的样式
  2. rowHeight:用于设置或返回表格的行高。建议用tableView:heightForRowAtIndexPath: 方法设置行高
  3. separatorStyle:用于设置或返回表格的分隔条样式
  4. separatorColor:设置分隔条的颜色
  5. backgroundView:返回或设置表格的背景控件
  6. tableHeaderView:该属性可设置或返回表格的页眉控件
  7. tableFooterView:页脚控件
  8. -numberOfRowsInSection:返回指定分区包含的行数
  9. -numberOfSections:返回表格所包含的分区数

UITableViewDataSource

  1. -tableView:cellForRowAtIndexPath:必需,返回UITableViewCell对象作为指定IndexPath对应表格的控件
  2. -tableView:numberOfRowsInSection:必需,返回的NSInteger决定指定分区包含的表格行数量
  3. -numberOfSectionsInTableVIew:可选,返回的NSInteger决定该表格所包含的分区数量。默认只包含一个分区
实例1. 简单表格

iOS UI控件7(UITableView)

#import "SimpleTableViewController.h"@interface SimpleTableViewController ()@property (weak, nonatomic) IBOutlet UITableView *simpleTable;@property (strong,nonatomic) NSArray * actors;@property(strong,nonatomic) NSArray * details;@property (strong ,nonatomic) NSArray* actorsImg;@end@implementation SimpleTableViewController@synthesize actors;@synthesize details;@synthesize actorsImg;-(void) viewDidLoad{  [super viewDidLoad];  actors = [NSArray arrayWithObjects:@"刘亦菲",@"柳岩",@"唐嫣",@"林志玲",@"汤唯",@"angelbaby", nil];  details = [NSArray arrayWithObjects:@"《仙剑奇侠传》",@"主持人",@"《何以笙箫默》",@"女神",@"《北京爱上西雅图》",@"《奔跑吧,兄弟》", nil];  actorsImg = [NSArray arrayWithObjects:@"lyf.jpg", @"ly.jpg",@"ty.jpg",@"lzl.jpg", @"tw.jpg",@"ab.jpg", nil];  //为TableView设置DataSource  self.simpleTable.dataSource = self;  //设置页眉  self.simpleTable.tableHeaderView = [[UIImageView alloc ] initWithImage:[UIImage imageNamed:@"header.png"]];  //设置页脚  self.simpleTable.tableFooterView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"footer.png"]];}-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  //为表格行定义一个静态字符作为标识符  static NSString* cellId = @"cellId";  //从可重用表格行的队列中取出一个表格行  UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];  if (cell == nil) {    switch (indexPath.row %4) {      case 0:        cell = [[UITableViewCell alloc]            initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];        break;      case 1:        cell = [[UITableViewCell alloc]            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];        break;      case 2:        cell = [[UITableViewCell alloc]            initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];        break;      case 3:        cell = [[UITableViewCell alloc]            initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellId];        break;      default:        break;    }  }  //将单元格的边框设置为圆角  cell.layer.cornerRadius = 12;  cell.layer.masksToBounds = YES;  //从IndexPath参数中获取当前行的行号  NSUInteger rowNo = indexPath.row;  cell.textLabel.text = [actors objectAtIndex:rowNo];  cell.imageView.image = [UIImage imageNamed:[actorsImg objectAtIndex:rowNo]];  //高亮状态是的图片  cell.imageView.highlightedImage = [UIImage imageNamed:@"1.jpg"];  cell.detailTextLabel.text = [details objectAtIndex:rowNo];  return cell;}//该方法的返回值决定分区宝航多个表格行-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  //只有一个分区  return actors.count;}@end

通过上面的代码,对UITableView进行简单的配置-dataSource/页眉/页脚。控制器实现了UITableViewDataSource协议中的两个方法,tableView:(UITableView *)tableView numberOfRowsInSection: 方法的返回值决定控件指定分区包含的表格行数量。方法(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:返回UITableVIewCell,将作为指定IndexPath对应表格行的UI控件,每个表格行都是一个UITableViewCell。

UITableViewCell包含的撒个配置属性:

  • textLabel:标题
  • detailTextLabel: 详细内容
  • image 左边的图标

访问表格控件的表格行和分区的方法:

  • -cellForRowAtIndexPath: 返回表格指定NSIndexPath对应的表格行
  • -indexPathForCell: 指定表格行对应的NSIndexPath
  • -indexPathForRowAtPoint: 返回表格中指定点所在NSIndexPath
  • -indexPathsForRowsInRect: 指定区域内所有NSIndexPath组成的数组
  • -visibleCells:所有可见区域内的表格行组成的数组
  • -indexPathsForVisibleRows: 可见区域内的表格行对应的NSIndexPath组成的数组

控制表格控件的滚动:

  • -scrollToRowAtIndexPath:atScrollPosition:animated:控制表格滚动到指定NSIndexPath对应的表格行的顶端、中间、下方
  • -scrollToNearestSelectedRowAtScrollPosition:animated:控制表格滚动到选择表格行的顶部、中间或下方

1.1 单元格的选中

UITableView 配置表格选中状态的属性:

  • allowsSelection 是否允许被选中
  • allowsMultipleSelection 允许被多选
  • allowsSelectionDuringEditing 编辑时是否允许被选中
  • allowMultipleSelectionDruingEditing 编辑时允许多选

操作选中行的方法:

  • -indexPathForSelectedRow: 获取选中行的IndexPath
  • -indexPathsForSelectedRows: 获取选中行的IndexPath组成的数组
  • -selectRowAtIndexPath:animated:scrollPosition: 控制表格选中指定NSIndexPath对应的表格行,最后一个参数控制是否滚动到被选中行的顶端、中间、底部
  • -deselectRowAtIndexPath:animated: 取消选中

响应表格行的选中事件,需哟啊实现委托对象UITableViewDelegate

  • -tableView:willSelectRowAtIndexPath:
  • -tableView:disSelectRowAtIndexPath:
  • -tableView:willDeselectRowAtIndexPath:
  • -tableView:didDeselectRowAtIndexPath:

1.2 定制表格行

可以通过如下方式定制表格行:

  1. 继承UITableViewCell定制表格行
  2. 使用动态单元格定制表格行
  3. 利用xib文件定义表格行
实例:继承UITableViewCell定制表格行

iOS UI控件7(UITableView)

主要实现逻辑:

在类接口定义部分定义两个Label用于外部访问

#import <UIKit/UIKit.h>@interface BookTableCellViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>@property (weak, nonatomic) IBOutlet UITableView *diyTableCell;@end

在类实现部分,重写initWithStyle 方法:

-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  if(self){    UIColor * bgColor = [UIColor colorWithRed:0.7 green:1.0 blue:0.7 alpha:1.0];    self.contentView.backgroundColor = bgColor;    UILabel* nameLabel = [[UILabel alloc]               initWithFrame:CGRectMake(5, 5, 70, 20)];    nameLabel.text=@"姓名:";    nameLabel.textAlignment = NSTextAlignmentRight;    nameLabel.font = [UIFont boldSystemFontOfSize:17];    nameLabel.backgroundColor = [UIColor clearColor];    [self.contentView addSubview:nameLabel];    UILabel* ageLabel = [[UILabel alloc]               initWithFrame:CGRectMake(5, 30, 70, 20)];    ageLabel.text = @"年龄:";    ageLabel.textAlignment = NSTextAlignmentRight;    ageLabel.font = [UIFont boldSystemFontOfSize:17];        ageLabel.backgroundColor = [UIColor clearColor];        [self.contentView addSubview:ageLabel];        self.nameTxt = [[UILabel alloc]            initWithFrame:CGRectMake(90, 5, 180, 20)];    self.nameTxt.textAlignment = NSTextAlignmentLeft;    self.nameTxt.font = [UIFont boldSystemFontOfSize:18];    self.nameTxt.textColor = [UIColor blueColor];    [self.contentView addSubview:self.nameTxt];        self.ageTxt = [[UILabel alloc]            initWithFrame:CGRectMake(90, 30, 180, 20)];    self.ageTxt.textAlignment = NSTextAlignmentLeft;    self.ageTxt.font = [UIFont boldSystemFontOfSize:18];    self.ageTxt.textColor = [UIColor blueColor];    [self.contentView addSubview:self.ageTxt];  }  return self;}

控制器类实现UITableViewDataSource,UITableViewDelegate,

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  static NSString* cellId = @"cellId";  BookTableCell* cell = [tableView dequeueReusableCellWithIdentifier:cellId];  if (cell == nil) {    cell = [[BookTableCell alloc]        initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];  }  NSUInteger rowNo = indexPath.row;  cell.layer.cornerRadius = 12;  cell.layer.masksToBounds = YES;    cell.nameTxt.text = [nameList objectAtIndex:rowNo];  cell.ageTxt.text = [ageList objectAtIndex:rowNo];  return cell;}
使用动态单元格原型定制表格行

Storyboard允许直接在UITableView设计单元格的外观。iOS UI控件7(UITableView)

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:iOS UI控件7(UITableView)

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。