第一:UITableView的基本使用
一、初始化方法
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;
//這個方法初始化表視圖的frame大小并且設置一個風格,UITableViewStyle是一個枚舉,如下:
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // 標準的表視圖風格
UITableViewStyleGrouped // 分組的表視圖風格
};
二、常用屬性
- 設置UITableView代理和數據源代理(注意:這一點至關重要)
@property (nonatomic, assign) id <UITableViewDataSource> dataSource;
@property (nonatomic, assign) id <UITableViewDelegate> delegate;
- 設置UITableViewCell高度(默認為44)(注意:當所有的Cell的高度相同可以有該屬性設置Cell高度;當Cell的高度不同是就不可以用該屬性設置了。)
@property (nonatomic)CGFloat rowHeight;
- 設置分區的頭視圖高度和尾視圖高度(當代理方法沒有實現時才有效)
@property (nonatomic) CGFloat sectionHeaderHeight;
@property (nonatomic) CGFloat sectionFooterHeight;
注意:這個屬性官方的解釋是如果你的UITableViewCell高度是可變的,那么設計一個估計高度可以加快代碼的運行效率。<NS_AVAILABLE_IOS(7_0) >
@property (nonatomic) CGFloat estimatedRowHeight;
- 下面這兩個屬性和上面相似,分別設置分區頭視圖和尾視圖的估計高度
@property (nonatomic) CGFloat estimatedSectionHeaderHeight; @property (nonatomic) CGFloat estimatedSectionFooterHeight;
- 設置分割線的位置 和分割線顏色
@property (nonatomic) UIEdgeInsets separatorInset;
@property (nonatomic, strong, nullable) UIColor *separatorColor
三、常用方法詳解
- 重載tableView
- (void)reloadData;
- 重載索引欄
- (void)reloadSectionIndexTitles;
// 這個方法常用語新加或者刪除了索引類別而無需刷新整個表視圖的情況下。
- 返回區數 (注意:如果不寫,默認是返回一個區)
- (NSInteger)numberOfSections;
- 根據區返回行數
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
- 獲取分區的大小(包括頭視圖,所有行和尾視圖)
- (CGRect)rectForSection:(NSInteger)section;
- 根據分區分別獲取頭視圖,尾視圖和行的高度
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
- 獲取某個點在tableView中的位置信息 和獲取某個cell在tableView中的位置信息
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
- 根據一個矩形范圍返回一個信息數組,數組中是每一行row的位置信息
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;
- 通過位置路徑獲取cell
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- 根據分區獲取頭視圖 和根據分區獲取尾視圖
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section;
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section;
- 使表示圖定位到某一位置(行)
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
// 注意:indexPah參數是定位的位置,決定于分區和行號。animated參數決定是否有動畫。scrollPosition參數決定定位的相對位置,它使一個枚舉,如下:
typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {
UITableViewScrollPositionNone,//同UITableViewScrollPositionTop
UITableViewScrollPositionTop,//定位完成后,將定位的行顯示在tableView的頂部
UITableViewScrollPositionMiddle,//定位完成后,將定位的行顯示在tableView的中間
UITableViewScrollPositionBottom//定位完成后,將定位的行顯示在tableView最下面
};
- 使表示圖定位到選中行
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
這個函數與上面的非常相似,只是它是將表示圖定位到選中的行。
四、tableView操作刷新塊的應用
在介紹動畫塊之前,我們先看幾個函數:
- 給UITableView插入分區
- (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這個方法,它會使動畫失效。
五、UITableView的編輯操作
- 設置是否是編輯狀態(編輯狀態下的cell左邊會出現一個減號,點擊右邊會劃出刪除按鈕)
@property (nonatomic, getter=isEditing) BOOL editing;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
- 設置cell編輯模式下選擇的狀態
//設置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
};
- 設置tableView頭視圖 、設置tableView尾視圖
@property (nonatomic, retain) UIView *tableHeaderView;
@property (nonatomic, retain) UIView *tableFooterView;
第二:UITbleViewCell滑動多按鈕
#pragma mark 編輯按鈕
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.myTableView setEditing:!self.myTableView.isEditing animated:YES];
}
#pragma mark 設置可以進行編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
#pragma mark 設置編輯的樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
#pragma mark 設置處理編輯情況
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//根據不同的編輯狀態做一些操作
}
#pragma mark 在滑動手勢刪除某一行的時候,顯示出更多的按鈕
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
//添加一個刪除按鈕
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
//移除要刪除的數據
[self.datas removeObjectAtIndex:indexPath.row];
//更新UITableView
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
// 添加一個置頂按鈕
UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了置頂");
//交換數據
[self.datas exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
//更新UITableView
[tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
}];
topRowAction.backgroundColor = [UIColor blueColor];
// 添加一個更多按鈕
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了更多");
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 將設置好的按鈕放到數組中返回
return @[deleteRowAction, topRowAction, moreRowAction];
}