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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市寝殴,隨后出現(xiàn)的幾起案子蒿叠,更是在濱河造成了極大的恐慌,老刑警劉巖蚣常,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件市咽,死亡現(xiàn)場離奇詭異,居然都是意外死亡抵蚊,警方通過查閱死者的電腦和手機施绎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贞绳,“玉大人谷醉,你說我怎么就攤上這事「员眨” “怎么了俱尼?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長萎攒。 經(jīng)常有香客問我遇八,道長,這世上最難降的妖魔是什么耍休? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任刃永,我火速辦了婚禮,結(jié)果婚禮上羹应,老公的妹妹穿的比我還像新娘。我一直安慰自己次屠,他們只是感情好园匹,可當(dāng)我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布雳刺。 她就那樣靜靜地躺著,像睡著了一般裸违。 火紅的嫁衣襯著肌膚如雪掖桦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天供汛,我揣著相機與錄音枪汪,去河邊找鬼。 笑死怔昨,一個胖子當(dāng)著我的面吹牛雀久,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播趁舀,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼赖捌,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了矮烹?” 一聲冷哼從身側(cè)響起越庇,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎奉狈,沒想到半個月后卤唉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡仁期,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年桑驱,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蟀拷。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡碰纬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出问芬,到底是詐尸還是另有隱情悦析,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布此衅,位于F島的核電站强戴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏挡鞍。R本人自食惡果不足惜骑歹,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望墨微。 院中可真熱鬧道媚,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至镀脂,卻和暖如春牺蹄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背薄翅。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工沙兰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人翘魄。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓鼎天,卻偏偏與公主長得像,于是被迫代替她去往敵國和親熟丸。 傳聞我的和親對象是個殘疾皇子训措,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容