實現(xiàn)列表有兩種方式 方式一繼承UIViewController,實現(xiàn)UITableViewDataSource和UITableViewDelegate協(xié)議谒所。聲明UITableView洗搂。UserInfoViewController.h@interface UserInfoViewController : UIViewController{
}
@end
UserInfoViewController.m
@interface UserViewController (){
}
@property(nonatomic,strong)UITableView *tableView;
@end
@implementation UserViewController
- (void)viewDidLoad {
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.heigh style:UITableViewStyleGrouped];
self.tableView.delegate=self;
self.tableView.dataSource=self;
[self.view addSubview:self.tableView];
}
@end
方式二
繼承UITableViewController,UITableViewController默認(rèn)實現(xiàn)UITableViewDataSource和UITableViewDelegate協(xié)議。默認(rèn)聲明UITableView载弄。
UserViewController.h
@interface UserInfoViewController : UITableViewController
@end
UserViewController.m
@interface UserInfoViewController (){
}
@end
@implementation UserInfoViewController
- (void)viewDidLoad {
}
@end
UITableViewDataSource
主要為UITableView提供顯示用的數(shù)據(jù)(UITableViewCell)耘拇,指定UITableViewCell支持的編輯操作類型(insert,delete和reordering)宇攻,并根據(jù)用戶的操作進行相應(yīng)的數(shù)據(jù)更新操作惫叛,如果數(shù)據(jù)沒有更具操作進行正確的更新,可能會導(dǎo)致顯示異常逞刷,甚至crush嘉涌。
必須實現(xiàn)的方法
//返回列表顯示行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
-
//返回每行顯示的UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
選擇實現(xiàn)的方法
//返回列表中Section的數(shù)量妻熊,默認(rèn)返回1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//設(shè)置標(biāo)題頭的名稱
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//設(shè)置標(biāo)題腳的名稱
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//是否支持對列表的行進行增,刪功能
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
//是否支持對列表進行的行進行移動順序功能
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
//根據(jù)編輯樣式調(diào)整數(shù)據(jù)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
//根據(jù)移動前后的NSIndexPath調(diào)整數(shù)據(jù)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
UITableViewDelegate
主要提供一些可選的方法仑最,用來控制tableView的選擇扔役、指定section的頭和尾的顯示以及協(xié)助完成cell的刪除和排序等功能。
/*-----自定義顯示,可以實現(xiàn)隔行顯示不同的顏色----*/
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
-
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
-
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
-
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
-
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
-
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
/*-----返回每行警医,表頭亿胸,表尾的高度----*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
/*-----返回每行,表頭预皇,表尾的預(yù)估高度----*/
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
/*-----custom view for header----*/
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
/*-----選中和取消選中----*/
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
/*-----設(shè)置編輯狀態(tài)時的樣式,可以返回
UITableViewCellEditingStyleInsert(表示增加)
UITableViewCellEditingStyleDelete(表示刪除)
UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert(表示多選)
----*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
/*-----設(shè)置刪除按鈕的名字----*/
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
/*-----設(shè)置滑動刪除時的多個按鈕----*/
- (nullable NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
UITableView不顯示多余的表格分割線
UIView *view =[ [UIView alloc]init];
view.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:view];
[tableView setTableHeaderView:view];
設(shè)置UITableView分割線間隔
我們在使用tableview時會發(fā)現(xiàn)分割線的左邊會短一些侈玄,通常可以使用 setSeparatorInset:UIEdgeInsetsZero 來解決吟温。但是升級到XCode6之后序仙,在iOS8里發(fā)現(xiàn)沒有效果。下面給出解決辦法:
首先在viewDidLayoutSubviews方法中加上如下代碼:
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0,10,0,10)];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsMake(0,10,0,10)];
}
}
然后在willDisplayCell方法中加入如下代碼:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsMake(0,10,0,10)];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsMake(0,10,0,10)];
}
}
UITableViewCell復(fù)用機制
我們通常編寫UITableViewCell的時候鲁豪,都會像下面代碼一樣聲明UITableViewCell潘悼,這樣編寫是為了解決什么問題呢?
static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)identifier#>]
if (cell == nil) {
<#statements#>
}
這個問題答案核心是這個機制要解決什么樣的問題呈昔。 關(guān)鍵點在"一個屏幕顯示的cell數(shù)量"是有限的 當(dāng)屏幕滾動時候挥等,就會調(diào)用方法獲取新的cell,而老的cell會在屏幕外面就不顯示了
reuse機制就是這樣堤尾。當(dāng)cell需要顯示的時候肝劲,從queue里面找,找到了郭宝,設(shè)置一下內(nèi)容辞槐,顯示出來 滾動界面當(dāng)有cell被移出屏幕時,把這個cell丟到queue里面 顯示新的cell時粘室,如果有“相同類型”(identifier)的cell榄檬,就從隊列拿一個出來,設(shè)置數(shù)據(jù)衔统,顯示出來 至于queue里面會有多少cell鹿榜,這個會自動控制
要注意的是,queue里面存儲的是cell的實例锦爵,不是“原型” 因此就會出現(xiàn)上面說的“假設(shè)每頁有 5個舱殿。 則 第6個復(fù)用第1個cell; 第7個復(fù)用第2個险掀;” 這樣的結(jié)果是不管你的table有多少行沪袭,內(nèi)存里實際上都只需要存儲一個屏幕那么多行的cell就搞定了。
獲取UITableViewCell相對于UITableView的坐標(biāo)
在使用 UITableViewCell 的frame屬性獲取origin得到的坐標(biāo)是不變的.也就是說如果UITableView初始化完畢后,每個cell的坐標(biāo)是固定的,x不變,y 隨index遞增的.
經(jīng)過測試發(fā)現(xiàn),任何一個cell拖拽或則滑動到UITableView的任意相對位置,cell的frame屬性都沒有改變.
那怎樣獲取UITableViewCell相對于UITableView的坐標(biāo)?
CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
UITableView通過長按手勢定位獲取indexPath
- (void)longPress:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan) {
//通過定位獲取indexPath
CGPoint point = [recognizer locationInView:self.tableView];
self.cellIndexPath=[self.tableView indexPathForRowAtPoint: point];
}
}
表格刷新
//整表刷新
[self.tableView reloadData];
//當(dāng)行刷新
[self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
表格刪除
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:[self.deleteDic allKeys]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
這兩個方法樟氢,是配合起來使用的冈绊,標(biāo)記了一個tableView的動畫塊侠鳄。分別代表動畫的開始開始和結(jié)束。兩者成對出現(xiàn)死宣,可以嵌套使用伟恶。
一般,在添加十电,刪除知押,選擇 tableView中使用,并實現(xiàn)動畫效果鹃骂。在動畫塊內(nèi)台盯,不建議使用reloadData方法,如果使用畏线,會影響動畫静盅。