天天看点

iOS中表视图(UITableView)使用详解(二)

四、tableView操作刷新块的应用

在介绍动画块之前,我们先看几个函数:

插入分区

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

animation参数是一个枚举,枚举的动画类型如下

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {

   UITableViewRowAnimationFade,//淡入淡出

   UITableViewRowAnimationRight,//从右滑入

   UITableViewRowAnimationLeft,//从左滑入

   UITableViewRowAnimationTop,//从上滑入

   UITableViewRowAnimationBottom,//从下滑入

   UITableViewRowAnimationNone,  //没有动画

   UITableViewRowAnimationMiddle,          

   UITableViewRowAnimationAutomatic = 100  // 自动选择合适的动画

};

删除分区

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

重载一个分区

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

移动一个分区

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

插入一些行

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

删除一些行

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

重载一些行

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

移动某行

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

了解了上面几个函数,我们来看什么是操作刷新块:

当我们调用的上面的函数时,tableView会立刻调用代理方法进行刷新,如果其中我们所做的操作是删除某行,而然数据源数组我们可能并没有刷新,程序就会崩溃掉,原因是代理返回的信息和我们删除后不符。

IOS为我们提供了下面两个函数解决这个问题:

开始块标志

- (void)beginUpdates;

结束快标志

- (void)endUpdates;

我们可以将我们要做的操作全部写在这个块中,那么,只有当程序执行到结束快标志后,才会调用代理刷新方法。代码示例如下:

[tab beginUpdates];

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

   [dataArray removeObjectAtIndex:1];

   [tab endUpdates];

注意:不要在这个块中调用reloadData这个方法,它会使动画失效。

五、tableView的编辑操作

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

@property (nonatomic, getter=isEditing) BOOL editing;                            

- (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;

六、选中cell的相关操作

获取选中cell的位置信息

- (NSIndexPath *)indexPathForSelectedRow;

获取多选cell的位置信息

- (NSArray *)indexPathsForSelectedRows;

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

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

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

注意:这两个方法将不会回调代理中的方法。

七、tableView附件的相关方法

设置索引栏最小显示行数

@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;                                                      

设置索引栏字体颜色

@property (nonatomic, retain) UIColor *sectionIndexColor;

设置索引栏背景颜色

@property (nonatomic, retain) UIColor *sectionIndexBackgroundColor;

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

@property (nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor;

设置分割线的风格

@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;

这个风格是一个枚举,如下:

typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {

   UITableViewCellSeparatorStyleNone,//无线

   UITableViewCellSeparatorStyleSingleLine,//有线

   UITableViewCellSeparatorStyleSingleLineEtched  

设置分割线颜色

@property (nonatomic, retain) UIColor           *separatorColor;

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

@property (nonatomic, copy) UIVisualEffect      *separatorEffect;

注意:这个属性是IOS8之后新的。

设置tableView头视图

@property (nonatomic, retain) UIView *tableHeaderView;  

设置tableView尾视图

@property (nonatomic, retain) UIView *tableFooterView;

从复用池中取cell

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

获取一个已注册的cell

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath

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

- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier;

通过xib文件注册cell

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

通过OC类注册cell

- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

上面两个方法是IOS6之后的方法。

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

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

- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)

关于tableView的代理方法,因为篇幅原因,总结在下一篇博客中。