實現(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默認實現(xiàn)UITableViewDataSource和UITableViewDelegate協(xié)議。默認聲明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ù)用戶的操作進行相應的數(shù)據(jù)更新操作,如果數(shù)據(jù)沒有更具操作進行正確的更新,可能會導致顯示異常,甚至crush。
必須實現(xiàn)的方法
//返回列表顯示行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
-
//返回每行顯示的UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
選擇實現(xiàn)的方法
//返回列表中Section的數(shù)量,默認返回1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//設置標題頭的名稱
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//設置標題腳的名稱
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//是否支持對列表的行進行增,刪功能
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
//是否支持對列表進行的行進行移動順序功能
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
//根據(jù)編輯樣式調整數(shù)據(jù)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
//根據(jù)移動前后的NSIndexPath調整數(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;
/*-----返回每行,表頭,表尾的預估高度----*/
- (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);
/*-----設置編輯狀態(tài)時的樣式,可以返回
UITableViewCellEditingStyleInsert(表示增加)
UITableViewCellEditingStyleDelete(表示刪除)
UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert(表示多選)
----*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
/*-----設置刪除按鈕的名字----*/
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
/*-----設置滑動刪除時的多個按鈕----*/
- (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];
設置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復用機制
我們通常編寫UITableViewCell的時候,都會像下面代碼一樣聲明UITableViewCell,這樣編寫是為了解決什么問題呢?
static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)identifier#>]
if (cell == nil) {
<#statements#>
}
這個問題答案核心是這個機制要解決什么樣的問題。 關鍵點在"一個屏幕顯示的cell數(shù)量"是有限的 當屏幕滾動時候,就會調用方法獲取新的cell,而老的cell會在屏幕外面就不顯示了
reuse機制就是這樣。當cell需要顯示的時候,從queue里面找,找到了,設置一下內容,顯示出來 滾動界面當有cell被移出屏幕時,把這個cell丟到queue里面 顯示新的cell時,如果有“相同類型”(identifier)的cell,就從隊列拿一個出來,設置數(shù)據(jù),顯示出來 至于queue里面會有多少cell,這個會自動控制
要注意的是,queue里面存儲的是cell的實例,不是“原型” 因此就會出現(xiàn)上面說的“假設每頁有 5個。 則 第6個復用第1個cell; 第7個復用第2個;” 這樣的結果是不管你的table有多少行,內存里實際上都只需要存儲一個屏幕那么多行的cell就搞定了。
獲取UITableViewCell相對于UITableView的坐標
在使用 UITableViewCell 的frame屬性獲取origin得到的坐標是不變的.也就是說如果UITableView初始化完畢后,每個cell的坐標是固定的,x不變,y 隨index遞增的.
經過測試發(fā)現(xiàn),任何一個cell拖拽或則滑動到UITableView的任意相對位置,cell的frame屬性都沒有改變.
那怎樣獲取UITableViewCell相對于UITableView的坐標?
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];
//當行刷新
[self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
表格刪除
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:[self.deleteDic allKeys]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
這兩個方法,是配合起來使用的,標記了一個tableView的動畫塊。分別代表動畫的開始開始和結束。兩者成對出現(xiàn),可以嵌套使用。
一般,在添加,刪除,選擇 tableView中使用,并實現(xiàn)動畫效果。在動畫塊內,不建議使用reloadData方法,如果使用,會影響動畫。