UITableView

1.继承关系

UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳。UITableView的代理协议也继承了UIScrollView的代理协议,可以通过实现UIScrollView的代理方法,监听UITableView的变化。在UITableView中没有列的概念,只有行的概念,数据都是按行显示的。

 

2.常用属性

// 数据源和代理

@property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource;


@property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;



// tableview的视图风格(只读)

@property (nonatomic, readonly) UITableViewStyle style;



// 设置 行高、区头高度、区尾高度(当代理方法没有实现时才有效)

@property (nonatomic) CGFloat rowHeight;


@property (nonatomic) CGFloat sectionHeaderHeight;


@property (nonatomic) CGFloat sectionFooterHeight;



// 如果你的tableView的行高是可变的,那么设计一个估计高度可以加快代码的运行效率。

@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0);


@property (nonatomic) CGFloat estimatedSectionHeaderHeight NS_AVAILABLE_IOS(7_0);


@property (nonatomic) CGFloat estimatedSectionFooterHeight NS_AVAILABLE_IOS(7_0);



// 设置分割线的位置(通过这个属性,可以手动设置分割线的位置偏移)

@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

例如:tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);//分割线边界对齐



// 设置tableView背景view视图

@property (nonatomic, strong, nullable) UIView *backgroundView;


3.常用方法详解


 3.1 数据刷新

// 刷新数据

- (void)reloadData;


// 这个方法常用于新加或者删除了索引类别而无需刷新整个表视图的情况下。

- (void)reloadSectionIndexTitles;


 3.2 信息

// 分区数

- (NSInteger)numberOfSections;


// 根据分区来获取所在区的行数

- (NSInteger)numberOfRowsInSection:(NSInteger)section;


// 获取分区的大小(包括头视图,所有行和尾视图)

- (CGRect)rectForSection:(NSInteger)section;



// 根据分区分别获取头视图,尾视图和行的高度

- (CGRect)rectForHeaderInSection:(NSInteger)section;


- (CGRect)rectForFooterInSection:(NSInteger)section;


- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;


// 获取某个点在tableView中的位置信息

- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;


// 获取某个celltableView中的位置信息

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;


// 根据一个矩形范围返回一个信息数组,数组中是每一行row的位置信息

- (NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;


// 通过位置路径获取cell

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;


// 获取所有可见的cell

- (NSArray *)visibleCells;


// 获取所有可见行的位置信息

- (NSArray *)indexPathsForVisibleRows;


// 根据分区获取头视图

- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);


// 根据分区获取尾视图

- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);


// 使表示图定位到某一位置()

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

 

注意:indexPah参数是定位的位置,决定于分区和行号。animated参数决定是否有动画。scrollPosition参数决定定位的相对位置,它使一个枚举,如下:

typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {

UITableViewScrollPositionNone, // 同下

UITableViewScrollPositionTop, // 定位完成后,将定位的行显示在tableView的顶部

UITableViewScrollPositionMiddle, // 定位完成后,将定位的行显示在tableView的中间

UITableViewScrollPositionBottom // 定位完成后,将定位的行显示在tableView的底部

};



// 使表示图定位到选中行

- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

 

-3.3 /行的插入、删除、刷新

 

// Row insertion/deletion/reloading.


// 开始、结束更新

- (void)beginUpdates;

 

在这里写 插入、删除、刷新、移动操作。


例如:

[tab beginUpdates];

[tab deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];

[dataArray removeObjectAtIndex:1];

[tab endUpdates];


- (void)endUpdates;



// 插入、删除、刷新、移动 一个分区

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;


- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;


- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;


- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;



// 插入、删除、刷新、移动 一些行

- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;


- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;


- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;


- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;


3.4 编辑操作

 

// 设置是否是编辑状态(编辑状态下的cell左边会出现一个减号,点击右边会划出删除按钮)

@property (nonatomic, getter=isEditing) BOOL editing;//(默认为NO)

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;


// 设置cell是否可以被选中(默认为YES)

@property (nonatomic) BOOL allowsSelection;


// 设置cell编辑模式下是否可以被选中

@property (nonatomic) BOOL allowsSelectionDuringEditing;


// 设置是否支持多选

@property (nonatomic) BOOL allowsMultipleSelection;


// 设置编辑模式下是否支持多选

@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);


3.5 选中cell的相关操作

// 获取选中cell的位置信息

- (NSIndexPath *)indexPathForSelectedRow;


// 获取多选cell的位置信息

- (NSArray *)indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0);


// 代码手动选中与取消选中某行

- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;


3.6 其他属性设置

// 设置索引栏最小显示行数

@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;


// 设置索引栏字体颜色

@property (nonatomic, strong, nullable) UIColor *sectionIndexColor;


// 设置索引栏背景颜色

@property (nonatomic, strong, nullable) UIColor *sectionIndexBackgroundColor;


// 设置索引栏被选中时的颜色

@property (nonatomic, strong, nullable) UIColor *sectionIndexTrackingBackgroundColor;


// 设置分割线的风格

@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;

/*

typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {

UITableViewCellSeparatorStyleNone,//无线

UITableViewCellSeparatorStyleSingleLine,//有线

UITableViewCellSeparatorStyleSingleLineEtched

};

*/


// 设置分割线颜色

@property (nonatomic, strong, nullable) UIColor *separatorColor;


// 设置分割线毛玻璃效果(IOS8之后可用)

@property (nonatomic, copy, nullable) UIVisualEffect *separatorEffect;



@property (nonatomic) BOOL cellLayoutMarginsFollowReadableWidth NS_AVAILABLE_IOS(9_0);


// 设置tableView头视图

@property (nonatomic, strong, nullable) UIView *tableHeaderView;


// 设置tableView尾部视图

@property (nonatomic, strong, nullable) UIView *tableFooterView;


// 从复用池中取cell

- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;


// 获取一个已注册的cell

- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);


// 从复用池获取头视图或尾视图

- (UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);


// 通过xib文件注册cell

- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier;


// 通过OC类注册cell

- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);


// 通过xib文件和OC类获取注册头视图和尾视图

- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);


- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

 


转载于:https://www.cnblogs.com/pan5008/p/11232586.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值