UITableView的基本知識


一、UITableView的概念:

UITableView 是iOS中最重要的控件,幾乎所有的頁面都可以用UITableView完成。

tableView的使用需要遵循代理和數據源,這也是一種非常棒的設計模式,數據源模式可以近似為代理模式。

tableview要引入2個代理UITableViewDelegate,UITableViewDataSource

二、UITableView的基本用法:

1、基本屬性:

(1)設置tableview的類型

UITableViewStylePlain? ? ? ? ? ? 基本類型,分區頭標題會懸浮

UITableViewStyleGrouped? ? 分組的類型,分區頭標題不會懸浮

//初始化:

UITableView? *tableView =? [[UITableView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)? style:UITableViewStylePlain];

(2)設置背景:

tableView.backgroundColor = [UIColor redColor];

(3)設置分割線:

類型:tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

顏色:tableView.separatorColor = [UIColor blackColor];

位置:tableView. separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);

(4)數據源和代理:

//主要管視圖內容

tableView.delegate = self;

//tableView里面的數據管理

tableView.dataSource = self;

[self.view addSubview:tableView];

2、設置 UITableViewDelegate的方法:

//每個section有多少rows:(必須實現的方法)

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

//返回指定的 row 的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

//返回指定的 section的header view 的高度:

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

//返回指定的 section的footer view 的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

//返回指定的row 的cell:(必須實現的方法)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

eg:

static NSString* cellname = @"cellname";

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//從tableveiw的復用池,是不是有可以復用的cell

//dequeueReusableCellWithIdentifier

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellname];

//初始化cell

if (cell == nil) {

//xib文件的初始化

cell = [[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil]lastObject];

}

//沒有點擊效果::

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.label.text = self.arr_tv[indexPath.row];

//自定義字體的uifont

cell.label.font = [UIFont fontWithName:self.arr_tv[indexPath.row] size:13];

//? ? NSLog(@"cellForRowAtIndexPath-%@",cell);

return cell;

}

//? 取消選中的效果::

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

[tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消選中

//其他代碼

}

//返回指定的 section 的header的高度;

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

// 設置section的數量,默認為1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

//section由兩部分組成,header和footer,分別設置各自需要展示的字符串,也可以自定義顯示的樣式,需要用到其他的方法

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

eg:- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

switch (section) {

case 0:

return [titleArray objectAtIndex:section];//提取標題數組的元素用來顯示標題

case 1:

return [titleArray objectAtIndex:section];//提取標題數組的元素用來顯示標題

default:

return @"Unknown";

}

}

// 哪些row可以被編輯

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

// Moving/reordering? 移動

// 哪些row可以被移動

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

//如何設置tableview 可以被編輯

[TableView setEditing:YES animated:YES];

如果要退出編輯模式,肯定就是設置為NO

// - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

返回當前cell? 要執行的是哪種編輯,下面的代碼是 返回 刪除? 模式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

return UITableViewCellEditingStyleDelete;

}

//? ? ? ? -(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

通知告訴用戶編輯了 哪個cell,對應上面的代碼,我們在這個函數里面執行刪除cell的操作。

-(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath{

[chatArray? removeObjectAtIndex:indexPath.row];

[chatTableView? reloadData];

}

//如何獲得 某一行的CELL對象

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

3、查看代理的方法:

// 設置對應的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// 自定義header和footer

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

// 設置編輯的樣式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容