【IOS 开发学习总结-OC-51】★★★ios开发之UI控件——UITableView 与UITableViewCell

【IOS 开发学习总结-OC-51】★★ios开发之UI控件——UITableView 与UITableViewCell

UITableView

UITableView,是个表格视图(实质上是列表),可以在表格行控件中添加多个子控件。可通过代码或者 IB 界面上拖拽添加该控件。

UITableView继承了 UIScrollView;这个UIScrollView主要封装了UIScrollViewCell 单元格控件。——默认状态下,可以对单元格进行滚动,所有的UITableViewController 实例被自动设为UIScrollView委托。

常见的表格视图效果(一般是单列效果,也可以做出多列效果):
这里写图片描述

UITableView常用属性说明

这里写图片描述

属性说明:
1. @property (nonatomic, readonly) UITableViewStyle style;——此属性有2个属性值,plain(普通风格)与 group(分组风格)
2. separatorStyle:——分割条样式。有3个属性如下,无样式;单线样式;和被蚀刻的样式(SingleLineEtched)三种。

typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched // This separator style is only supported for grouped style table views currently
};

  1. selection:——3个属性值,不允许选中,只允许单选,允许多选。

    @property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);// default is YES. Controls whether rows can be selected when not in editing mode
    @property (nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0); // default is NO. Controls whether multiple rows can be selected simultaneously

  2. editing:3种属性选择:①NO selection during editing:——编辑状态不允许选中;②single selection during editing:——编辑状态只允许单选;③multiple selection during editing:——编辑状态允许多选。

  3. separatorColor:设置分割条颜色;

  4. @property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource;——数据源

  5. @property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;——代理委托
  6. @property (nonatomic) CGFloat rowHeight;——行高 // will return the default value if unset
  7. @property (nonatomic) CGFloat sectionHeaderHeight;——页眉高度 // will return the default value if unset
  8. @property (nonatomic) CGFloat sectionFooterHeight;——页脚高度 // will return the default value if unset
  9. @property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0);——估算行高// default is 0, which means there is no estimate
  10. @property (nonatomic) CGFloat estimatedSectionHeaderHeight NS_AVAILABLE_IOS(7_0);—— 估算的页眉高度 // default is 0, which means there is no estimate
  11. @property (nonatomic) CGFloat estimatedSectionFooterHeight NS_AVAILABLE_IOS(7_0);——古都按的页脚高度 // default is 0, which means there is no estimate
  12. @property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; ——单元格分割符定制 // allows customization of the frame of cell separators
  13. @property (nonatomic, strong, nullable) UIView *backgroundView NS_AVAILABLE_IOS(3_2);——背景视图 // the background view will be automatically resized to track the size of the table view. this will be placed as a subview of the table view behind all cells and headers/footers. default may be non-nil for some devices.

UITableView常用方法

  1. - (NSInteger)numberOfRowsInSection:(NSInteger)section;——指定分区包含的行数;
  2. @property (nonatomic, readonly) NSInteger numberOfSections;——表格所包含的分区数;
  3. - (void)reloadData;// reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks
  4. - (void)reloadSectionIndexTitles NS_AVAILABLE_IOS(3_0); // reloads the index bar.
  5. - (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;// Used by the delegate to acquire an already allocated cell, in lieu of (代替)allocating a new one.

UITableView的UITableViewDataSource

UITableView与 UIPickerView 控件类似,UITableView只负责通用行为,而由UITableViewDataSource提供,分区数,每个分区有多少表格行,各表格行对应的 UI控件等内容。

使用UITableViewDataSource 前要在头文件中实现UITableViewDataSource协议。然后可以实现如下必须和可选的方法:

@protocol UITableViewDataSource<NSObject>

@required

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;//——指定分区的表格行数

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;//返回UITableViewCell对象作为指定索引对应表格行的控件

@optional

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;//——分区数,如果不调用该方法的话,默认值为1个分区              // Default is 1 if not implemented

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // —— 分区的表头/页眉标题   fixed font style. use custom view (UILabel) if you want something different

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
// —— 分区的页脚/表尾标题

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;//是否可以编辑指定索引处的表格行

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;//是否可以移动指定索引处的表格行

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView; // ——各分区标题的索引  return list of section titles to display in section index view (e.g. "ABCD...Z#")

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;  // tell table which section corresponds(符合) to section title/index (e.g. "B",1)),官方解释:Asks the data source to return the index of the section having the given title and section title index.

// Data manipulation - insert and delete support

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;//——提交编辑风格

// Data manipulation - reorder / moving support

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;//——从一个指定索引的行移动到另一个指定索引所在的行    
//官方解释:Tells the data source to move a row at a specific location in the table view to another location.

@end

UITableViewDataSource的使用步骤

  1. 代码或者 IB 界面添加一个UITableView控件;
  2. 为UITableView添加 dataSource 属性——必须是实现UITableViewDataSource协议的对象。
  3. 让指定类 (通常为控制器类)实现UITableViewDataSource协议,并实现协议中的方法。

UITableView的UITableViewDelegate

当程序需要响应表格行的选中事件时,需要借助UITableView的委托对象——该对象必须实现UITableViewDelegate协议——当表格行发生选中事件时,都会激发该委托对象的响应方法。UITableViewDelegate定义了如下方法:

- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;//将要选中表格中某行时激发该方法

- (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);//将要取消选中表格某行时激发

// Called after the user changes the selection.
- (void)tableView:(UITable
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值