UITableView(表视图)
2016-05-05 10:44
423 查看
UITableView
@interface UITableView : UIScrollView//表视图风格 typedef NS_ENUM(NSInteger, UITableViewStyle) { UITableViewStylePlain, 平铺 UITableViewStyleGrouped 成组 }; //分割线颜色 @property (nonatomic, strong, nullable) UIColor *separatorColor //分割线风格 @property (nonatomic) UITableViewCellSeparatorStyle separatorStyle; // default is UITableViewCellSeparatorStyleSingleLine typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) { //没有分割线 UITableViewCellSeparatorStyleNone, //单行线 UITableViewCellSeparatorStyleSingleLine, //没有分割线 UITableViewCellSeparatorStyleSingleLineEtched }; //行高 @property (nonatomic) CGFloat rowHeight; //头视图 @property (nonatomic, strong, nullable) UIView *tableHeaderView; //脚视图 @property (nonatomic, strong, nullable) UIView *tableFooterView;
//初始化 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style ; //根据indexPath获取tableViewCell - (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; //根据tableViewCell获取获取indexPath - (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell; //获取所有可见的tableViewCells @property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *visibleCells; //获取所有可见的indexPaths @property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForVisibleRows; //把指定位置滚动到对齐的位置 - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; typedef NS_ENUM(NSInteger, UITableViewScrollPosition) { UITableViewScrollPositionNone, 不对齐 UITableViewScrollPositionTop, 顶部对齐 UITableViewScrollPositionMiddle, 中部对齐 UITableViewScrollPositionBottom 底部对齐 }; //重新加载数据 - (void)reloadData; //进入编辑状态 - (void)setEditing:(BOOL)editing animated:(BOOL)animated;
UITableViewDataSource
@protocol UITableViewDataSource@required //设置行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; //设置组数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //设置索引(右边拦) - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED; //创建单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; //进行单元格的交换调用的方法 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath; //插入或者删除单元格调用的方法 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; //根据行数定义编辑模式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; //设定可以移动的行数 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; //移动单元格 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
static NSMutableArray fonts = [[UIFont familyNames] mutableCopy];
根据编辑动作实现方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath { /* UITableViewCellEditingStyleNone, 默认状态 UITableViewCellEditingStyleDelete, 删除状态 UITableViewCellEditingStyleInsert 插入状态 */ //判断编辑的状态,是否为删除 if (editingStyle == UITableViewCellEditingStyleDelete) { NSLog(@"删除"); //1:删除数组中对应的一项 [fonts removeObjectAtIndex:indexPath.row]; //2:删除单元格(带动画效果) //IndexPaths数组 将所有需要删除的单元格的索引放入这个数组中 [tbView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; }else if (editingStyle == UITableViewCellEditingStyleInsert) { //输入数据 [fonts insertObject:@"abcd" atIndex:indexPath.row]; //插入单元格 [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom]; } }
设定可以进行编辑的行数
//设定可以编辑的单元格 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ //行数在10个之前的都可以进入编辑状态 if (indexPath.row < 10) { return YES; } return NO; }
设置可以移动的行数
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row < 10) { return YES; } return NO; }
实现移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ //改变数据中的顺序 //1:拿出数据 id object = [fonts objectAtIndex:sourceIndexPath.row]; //2:删除数据 [fonts removeObjectAtIndex:sourceIndexPath.row]; //3:插入到新的位置去 [fonts insertObject:object atIndex:destinationIndexPath.row]; }
UITableViewDelegate
@protocol UITableViewDelegate//根据indexPath设置行高 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; //设置头视图的高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; //设置尾视图的高度 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; //设置头视图的标题(使用默认头视图可以设置) - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; //设置尾视图的高度(使用默认尾视图时可以设置) - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; //设置头视图 - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; //设置尾视图 - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; //选中单元格调用的方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; //没有选中的单元格调用的方法 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
NSIndexPath
@interface NSIndexPath (UITableView)//行数 @property (nonatomic, readonly) NSInteger section; //索引数 @property (nonatomic, readonly) NSInteger row;
+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
UITableViewCell
@interface UITableViewCell : UIView//视图 @property (nonatomic, readonly, strong, nullable) UIImageView *imageView; //背景视图 @property (nonatomic, strong, nullable) UIView *backgroundView //内容视图 @property (nonatomic, readonly, strong) UIView *contentView; //选中的单元格背景视图 @property (nonatomic, strong, nullable) UIView *selectedBackgroundView; //辅助样式 @property (nonatomic) UITableViewCellAccessoryType accessoryType; // default is UITableViewCellAccessoryNone. use to set standard type typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) { UITableViewCellAccessoryNone, UITableViewCellAccessoryDisclosureIndicator, UITableViewCellAccessoryDetailDisclosureButton, UITableViewCellAccessoryCheckmark, UITableViewCellAccessoryDetailButton }; //文本标签 @property (nonatomic, readonly, strong, nullable) UILabel *textLabel; //子标题标签 @property (nonatomic, readonly, strong, nullable) UILabel *detailTextLabel;
//初始化 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier; //单元格样式 typedef NS_ENUM(NSInteger, UITableViewCellStyle) { UITableViewCellStyleDefault, //默认 UITableViewCellStyleValue1, //图片显示,子标题在最右边 UITableViewCellStyleValue2, //图片不显示,子标题在标题后边 UITableViewCellStyleSubtitle //图片显示,子标题在标题下边 };
相关文章推荐
- VM10.0.1 build-1379776安装android-x86-4.4-r5.iso虚拟机 启动时error17的处理办法
- 贝塞尔曲线&CAShapeLayer超炫动画
- WRONGTYPE Operation against a key holding the wrong kind of value
- IOS中UITableViewCell使用详解
- 【Android安全】【Android】使用dex2jar 与JD-Gui 反编译APK文件,查看源代码
- iOS开发之自定义UITextField的方法
- easyui datagrid 点击表头排序问题
- Caused by: java.lang.IllegalArgumentException: setAttribute: Non-serializable attribute questionList
- 深入分析Parquet列式存储格式
- SCVMM-02 Install Virtual Guest Services
- STL系列之一 deque双向队列
- EasyUI Tabs全攻略
- iOS巧用UIButton自定义控件
- uva 10935 throwing cards away <queue>
- String、StringBuffer与StringBuilder之间区别
- iOS中使用UIDatePicker制作时间选择器的实例教程
- CRM开发要点(二)
- Java 连接hive2 server 通过jdbc 出现了问题 Required field 'client_protocol' is unset! Struct:TOpenSessionReq
- 浅谈自定义UITextField的方法
- you must configure in a separate build directory