IOS-UITableView開發(fā)常用各種方法總結

實現(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方法,如果使用,會影響動畫。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,030評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,310評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,951評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,796評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,566評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,055評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,142評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,303評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 48,799評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,683評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,899評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,409評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,135評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,520評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,757評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,528評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,844評論 2 372

推薦閱讀更多精彩內容