UITableView那些事

UITableView那些事<一>   

tableview是iOS开发中常用的控件之一,也是出现问题比较多的控件。所以闲暇时间,我全面的写一下。
第一篇就先写写tableview的各种属性和方法吧

一、Configuring a TableView

1.初始化方法   - ( instancetype ) initWithFrame: ( CGRect ) frame  style: ( UITableViewStyle ) style

2.样式属性  
@property ( nonatomic ,   readonly )   UITableViewStyle  style

3.返回section下得row的个数    - ( NSInteger ) numberOfRowsInSection: ( NSInteger ) section

4.返回section的个数(只读)
@property ( nonatomic ,   readonly )   NSInteger  numberOfSections

5.cell的高度   @property ( nonatomic )   CGFloat  rowHeight

6.设置分割线   @property ( nonatomic )   UITableViewCellSeparatorStyle  separatorStyle
有3种样式 UITableViewCellSeparatorStyleNone,          UITableViewCellSeparatorStyleSingleLine, UITableViewCellSeparatorStyleSingleLineEtched
   默认第二种,1、3都是不显示分割线的,3只支持分组的情况,这个属性用的不多,用tableview直接调用即可。

7.设置分割线的颜色  @property ( nonatomic ,   strong )   UIColor  * separatorColor

8.分割线设置毛玻璃效果   @property ( nonatomic ,   copy )   UIVisualEffect  * separatorEffect
UIBlurEffect  *blurEffect = [ UIBlurEffect   effectWithStyle : UIBlurEffectStyleLight ];
  UIVibrancyEffect *vibrancyEffect = [ UIVibrancyEffect   effectForBlurEffect :blurEffect];
_tableView . separatorEffect  = vibrancyEffect;
  我看着效果不大,这个用的更少,,,iOS8以后使用

9.设置tableview的背景图片  
@property ( nonatomic ,   strong )   UIView  * backgroundView

10.设置分割线的填充   @property ( nonatomic )   UIEdgeInsets  separatorInset
_tableView . separatorInset  =  UIEdgeInsetsMake ( 0 3 0 11 );如果你直接这样设置,用处不大,分割线的左边不会顶格,右边也不会无限的缩进 这是因为cell有个属性              preservesSuperviewLayoutMargins 防止继承tableview的 margin设置 解决方法:
-( void ) tableView :( UITableView   *) tableView willDisplayCell :( UITableViewCell   *) cell forRowAtIndexPath :( NSIndexPath   *) indexPath [pre] {
// Remove seperator inset
if([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsMake(0800120)];
}
// Prevent the cell from inheriting the Table View's margin settings
if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell's layout margins
if([cell respondsToSelector:@selector(setLayoutMargins:)]){
[cell setLayoutMargins:UIEdgeInsetsMake(0800120)];
}
}  这样  分割线的长度 随意控制 想怎么改变怎么改变

11.这也是关于 分割线的一个设置 @property(nonatomic) BOOL cellLayoutMarginsFollowReadableWidth
if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
_tableView.cellLayoutMarginsFollowReadableWidth = NO;
   }这个设置iOS9以后才有,好像主要针对iPad,不设置的话,分割线左侧空出很多,不做太多探究

二、Creating TableViewCells
1. 用xib注册cell - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier


2.
 用类注册cell  - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier


3. cell的重用根据identifie,可以根据indexPath确定那个cell 
- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifie forIndexPath:(NSIndexPath *)indexPath


4. 
- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier

三、Accessing Header and Footer Views

1.  (功能同上) - ( void ) registerNib: ( UINib * ) nib  forHeaderFooterViewReuseIdentifier: ( NSString * ) identifier

2.
 (功能同上) - ( void ) registerClass: ( Class ) aClass  forHeaderFooterViewReuseIdentifier: ( NSString * ) identifier

3.     用于注册过的headerView或则footerView                  
- ( __kindofUITableViewHeaderFooterView * ) dequeueReusableHeaderFooterViewWithIdentifier: ( NSString* ) identifier

4. @property ( nonatomic ,   strong )   UIView  * tableHeaderView

5. @property ( nonatomic ,   strong )   UIView  * tableFooterView

6. @property ( nonatomic )   CGFloat  sectionHeaderHeight

7. @property ( nonatomic )   CGFloat  sectionFooterHeight

8. - ( UITableViewHeaderFooterView * ) headerViewForSection: ( NSInteger ) section

 9.
- ( UITableViewHeaderFooterView * ) footerViewForSection: ( NSInteger ) section
上面几个属性和方法大家一目了然,没有什么说的

四、 Accessing cells and Sections

1. - (__kindofUITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

2.获取cell的indexPath  - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
  比如cell上面有个button,点击button的时候,可以获取button所在cell的indexPath
- (void)btnAction:(UIButton *)btn{
TableViewCell*cell = (TableViewCell*)[[btn superviewsuperview];
NSIndexPath*indexPath = [_tableViewindexPathForCell:cell];
NSLog@"%@",indexPath);
   } TableViewCell我是用的xib

3.根据给的point返回cell的indexPath - (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
只返回一个indexPath,比如cell的高度是80,point是{0,280},indexPath.row = 3

4.返回rect区域内的所有的indexPath的集合 - (NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect

5.返回屏幕中所有可见的cell的数组 @property(nonatomic, readonly) NSArray <__kindof UITableViewCell *> *visibleCells

6. 同上 都是返回屏幕中可见cell的数组 @property(nonatomic, readonly) NSArray <NSIndexPath *> *indexPathsForVisibleRows

五、 Estimating Element Heights

1.  @property(nonatomic) CGFloat estimatedRowHeight

2.@property(nonatomic) CGFloat estimatedSectionHeaderHeight

3.@property(nonatomic) CGFloat estimatedSectionFooterHeight
  上面这3个属性都是,预估高度值,加载cell的时候,系统先去判断cell的高度,浪费大量的时间,有多少cell就会加载多少次cell高度的方法。
  用预估值会节省加载时间,但是这样也会出现一个问题,
  以cell为例,系统会根据 estimatedRowHeight创建cell,到真正显示的时候,系统会根据 self-sizing计算出size,并调整tableView的 content size,
绘制cell。给个链接,有兴趣的可以看下,http://www.tuicool.com/articles/mQjeM3z

五、 Scrolling the TableView

1. 自动滚动tableView到你指定的cell处   - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:   (UITableViewScrollPosition)scrollPosition animated:(BOOL)animated 这里还有一种方法滚动tableview,就是他的父类scrollview的方法   setContentOffset

2.在cell选中的方法里写这个方法,选中cell的时候会滚动到你指定的地方   - (void)scrollToNearestSelectedRowAtScrollPosition:   (UITableViewScrollPosition)scrollPosition  animated: ( BOOL ) animated

六、Managing Selections

1.得到选中的cell的indexPath @property(nonatomic, readonly) NSIndexPath *indexPathForSelectedRow

2.同上 @property(nonatomic, readonly) NSArray <NSIndexPath *> *indexPathsForSelectedRows

3. 自动选中你所指定的indexPath的cell,并且滑动到那个cell       - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated: (BOOL)animated scrollPosition: (UITableViewScrollPosition)scrollPosition

4.当点击cell的时候push到下个VC的时候,再回来还是选中状态,这个函数就是返回后取消选中状态  
  - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated

5.是否允许cell被选中 @property(nonatomic) BOOL allowsSelection

6.是否允许cell多选  @property(nonatomic) BOOL allowsMultipleSelection

7.cell在编辑模式下,是否允许被选中@property(nonatomic) BOOL allowsSelectionDuringEditing

8.cell在编辑模式下,是否允许被多选@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing

七、Inserting,Deleting,and Moving Rows and Sections

1.  - (void)beginUpdates
2.- (void)endUpdates 上面两个方法成对使用,是在cell删除、添加等的时候使用的动画

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

4.   - (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
5.- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
6. - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
7.- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
8.- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection
上面这几个方法,看名字就知道什么意思了,就是cell的添加、删除、移动

八、Managing the Editing of Table Cells

1.  @property(nonatomic, getter=isEditing) BOOL editing

2. - (void)setEditing:(BOOL)editing animated:(BOOL)animate
     上面两个方法,设置cell是否可被编辑

九、Reloading the TableView

1.  刷新整个表视图 - (void)reloadData

2.刷新指定的indexPath,可以是数组 - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPathswithRowAnimation:(UITableViewRowAnimation)animation

3.刷新指定的section - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

4. 刷新索引  - (void)reloadSectionIndexTitles


十、Accessing Drawing Areas of the TableView

1. 返回指定的section的坐标 - (CGRect)rectForSection:(NSInteger)section

2.返回指定的indexPath的坐标 - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath


3.返回指定section的footerView的坐标 - (CGRect)rectForFooterInSection:(NSInteger)section


4.
返回指定section的headerView的坐标  - (CGRect)rectForHeaderInSection:(NSInteger)section

十一、Managing the Delegate and the DataSource

1.@property(nonatomic, weak) id< UITableViewDataSource > dataSource

2.@property(nonatomic, weak) id< UITableViewDelegate > delegate

十二、Configuring the Table Index

1.Index Row Limit属性与UITableView的sectionIndexMinimumDisplayRowCount有关,这个属性的含义是:"当行数达到某个数值,则显示索引栏"。(索引栏是通      过 sectionIndexTitlesForTableView方法来实现的)@property(nonatomic) NSInteger sectionIndexMinimumDisplayRowCount

2.改变索引栏字体的颜色@property(nonatomic, strong) UIColor *sectionIndexColor

3.改变索引栏的背景颜色@property(nonatomic, strong) UIColor *sectionIndexBackgroundColor

4.改变点击索引栏的背景颜色(点击之后恢复原状态)@property(nonatomic, strong) UIColor *sectionIndexTrackingBackgroundColor

十三、Managing Focus

1.@property(nonatomic) BOOL remembersLastFocusedIndexPath

十四、 Notifications

1.UITableViewSelectionDidChangeNotification

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值