UITableView

介紹:UITabelView是iOS開發(fā)中最常用, 也是最靈活的控件.用途最廣泛.

兩種風(fēng)格:

UITableViewStylePlain 和 UITableViewStyleGrouped。

UITableView中只有行的概念,每一行就是一個UITableViewCell。UITableViewCell內(nèi)置好的控件contentView父控件

contentView = (textLabel,detailTextLabel)+ UIImage控件(imageView)分別用于容器、顯示內(nèi)容、詳情和圖片。

代理方法、數(shù)據(jù)源方法:<UITableviewDelegate,UITableviewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView )tableView? ? ? numberOfRowsInSection:(NSInteger)section{? ? ? ? return 5;}

//有多少組(默認(rèn)為1)

- (UITableViewCell )tableView cellForRowAtIndexPath:(? NSIndexPath identifily = @"cellIdentifily";? ? ? UITableViewCell )tableView willDisplayCell:(UITableViewCell )indexPath{? ? ? ? ? NSLog(@"willDisplayCell");}?//cell內(nèi)容設(shè)置,屬性設(shè)置

- (void)tableView:(UITableView )cell forRowAtIndexPath:(NSIndexPath)tableView? ? didEndDisplayingHeaderView:(UIView )tableView? ? didEndDisplayingFooterView:(UIView )tableView? ? heightForRowAtIndexPath:(NSIndexPath )tableView? ? heightForHeaderInSection:(NSInteger)section{? ? ? ? ? ? ? return 15.0f;} ?//滑動時,cell消失時調(diào)用

點擊cell時調(diào)用

-?(void)tableView:(UITableView?)tableView?didSelectRowAtIndexPath:(NSIndexPath?)indexPath;

離開點擊時調(diào)用

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

重用機(jī)制->滾筒原理->只有固定->屏的視圖超出后重用這一屏的視圖一直滾動循環(huán)使用只需更改上面的數(shù)據(jù) ?DataSource有兩個必須實現(xiàn)的代理方法沒有實現(xiàn)就會出現(xiàn)警告——>運行崩潰

Required:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;//行數(shù)

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;//cell展示內(nèi)容


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;設(shè)置表頭的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;//設(shè)置表尾的高度

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;//設(shè)置表頭的視圖

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;//設(shè)置表尾的視圖

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;//設(shè)置表頭的標(biāo)題

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;//設(shè)置表尾的標(biāo)題

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{return @"首頁";}//設(shè)置表頭的標(biāo)題

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{return100;}//設(shè)置表尾的高度

//每個分組上邊預(yù)留的空白高度

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section

{return20;}

刷新數(shù)據(jù)

刷新整個TableVIew[self.tableView reloadData];

1.刷新一個cell、

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];[self.tableViewreloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationFade];

2.刷新一個section

NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];[self.tableView reloadSections:indexSetwithRowAnimation:UITableViewRowAnimationFade];

轉(zhuǎn)載:

iOS tableview那些事

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容