日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

简述UITableView的属性和用法

發布時間:2023/12/29 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简述UITableView的属性和用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

UITableView內置了兩種樣式:UITableViewStylePlain,UITableViewStyleGrouped

<UITableViewDataSource,UITableViewDelegate>里的方法:

tableView處理步驟

?1.有多少組

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

2.第section組頭部控件有多高

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

3.第section組有多少行

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

4.indexPath這行的cell有多高

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

5.indexPath這行的cell長什么樣子

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

6.第section組頭部顯示什么控件

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

?

//每當有一個cell進入視野屏幕就會調用,所以在這個方法內部就需要優化。

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

{

  if(cell==nil){

???   ?//在這里面做創建的工作。循環優化。防止刷新cell進入屏幕的時候重復的創建

  }

}

?

//當調用reloadData的時候,會重新刷新調用數據源內所有方法,其他事情都不會做呀

?[self reloadData]

?

?//這個方法只有在一開始有多少條數據才會算多少個高度,這個方法只會調用一次,但是每次reloadData的時候也會調用

?//而且會一次性算出所有cell的高度,比如有100條數據,一次性調用100次

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

?

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView //右側索引

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //行點擊事件

?NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //獲得被選中的indexPath可以得到section,row

??

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone]; //刷新table指定行的數據

????????

?? [self.tableView reloadData]; //刷新table所有行的數據

?

UITableView常用屬性:

??? UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain]; // 初始化表格

??? 分隔線屬性

tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //UITableViewCellSeparatorStyleNone;

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //取消分隔線

tableView.separatorColor = [UIColor lightGrayColor];

???

? ?// 條目多選

tableView.allowsMultipleSelection = YES;

????

??? // 設置標題行高

[_tableView setSectionHeaderHeight:kHeaderHeight];

[_tableView setSectionFooterHeight:0];

?

??? // 設置表格行高

?[_tableView setRowHeight:50];

?

//設置背景色

self.tableView.backgroundView? 優先級高,如果要設置backgroundColor的時候要先把view設置為nil

self.tableView.backgroundColor = [UIColor redColor];

?

//在tableView的頭部或者尾部添加view,footerView寬度是不用設置的

?????? xxxView.bounds = CGRectMake(0,0,0,height);

???  self.tableView.tableFooterView =xxxView;

?????? self.tableView.tableHeaderView =xxxView;

?

增加tableview滾動區域

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, xx, 0);?

UITableViewCell

//創建UITableViewCell??????? ?

UITableViewCell *cell = [[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

[cell.textLabel setBackgroundColor:[UIColor clearColor]];// 清空標簽背景顏色

cell.backgroundView =xx; //設置背景圖片

cell.backgroundVColor =xx;

cell.selectedBackgroundView = selectedBgView; //設置選中時的背景顏色

?

cell.accessoryView = xxxView; //設置右邊視圖

[cell setAccessoryType:UITableViewCellAccessoryNone]; //設置右側箭頭?

?

[self setSelectionStyle:UITableViewCellSelectionStyleNone]; //選中樣式

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

?

//設置cell的高度

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

?

contentView下默認有3個子視圖,其中的2個是UILabel,通過textLabel和detailTextLabel屬性訪問,第3個是UIImageView,通過imageView屬性訪問.

UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle

?

#pragma mark - 重新調整UITalbleViewCell中的控件布局

- (void)layoutSubviews

{

  [super layoutSubviews];

}

cell 里面還有一個contentView

UITableViewCell表格優化

UITableViewCell對象的重用原理:

重 用原理:當滾動列表時,部分UITableViewCell會移出窗口,UITableView會將窗口外的 UITableViewCell放入一個對象池中,等待重用。當UITableView要求dataSource返回UITableViewCell 時,dataSource會先查看這個對象池,如果池中有未使用的UITableViewCell,dataSource會用新的數據配置這個 UITableViewCell,然后返回給UITableView,重新顯示到窗口中,從而避免創建新對象

還有一個 非常重要的問題:有時候需要自定義UITableViewCell(用一個子類繼承 UITableViewCell),而且每一行用的不一定是同一種UITableViewCell(如短信聊天布局),所以一個UITableView可 能擁有不同類型的UITableViewCell,對象池中也會有很多不同類型的UITableViewCell,時可能會得到錯誤類型的 UITableViewCell那么UITableView在重用UITableViewCell。解決方案:UITableViewCell有個 NSString *reuseIdentifier屬性,可以在初始化UITableViewCell的時候傳入一個特定的字符串標識來設置 reuseIdentifier(一般用UITableViewCell的類名)。當UITableView要求dataSource返回 UITableViewCell時,先通過一個字符串標識到對象池中查找對應類型的UITableViewCell對象,如果有,就重用,如果沒有,就傳 入這個字符串標識來初始化一個UITableViewCell對象

?

單元格優化

?1. 標示符統一,使用static的目的可以保證表格標示符永遠只有一個

?2. 首先在緩沖池中找名為"myCell"的單元格對象

?3. 如果沒有找到,實例化一個新的cell

?

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

{

???  static NSString *cellIdentifier = @"myCell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

   //使用這種方法不用判斷下面的cell

???  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

???  if (cell == nil) {???????

???????    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

  ? ?}

   return cell;

}

?

表格的編輯模式

刪除、插入

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;? 開啟表格編輯狀態

?

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

{

? ? ? ?// 返回表格編輯編輯樣式。不實現默認都是刪除

  return editingStyle : UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert

}

?

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

??? ??// 根據editingStyle處理是刪除還是添加操作完成刪除、插入操作刷新表格

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

?

-(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

}

?

移動

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

sourceIndexPath 移動的行????

destinationIndexPath 目標的行

?

自定義表格行UITableViewCell

storyboard方式創建:

直接拖到UITableView里面設置UITableViewCell

注意:

1.通過XIB或者Storyboard自定義單元格時,在xib和Storyboard里面需要指定單元格的可重用標示符Identifier

?

2.注意表格的優化中的差別

在Storyboard中兩者等效

xxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

xxCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

?

在xib文件中有差別:

第一種情況,只能在iOS 6以上使用,如果在viewDidLoad注冊了nib文件,并且指定了“單元格”的可重用標示符,那么????

???? dequeueReusableCellWithIdentifier:

???? dequeueReusableCellWithIdentifier:forIndexPath:

???? 方法是等效的。如果在viewDidLoad中注冊了nib文件,表格緩沖池中的管理,有系統接管!

?

第二種情況,是在iOS 4以上均可以使用,如果沒有在viewDidLoad注冊nib文件,那么,只能使用

???? dequeueReusableCellWithIdentifier:并且需要判斷cell沒有被實例化,并做相應的處理

?

?

在代碼創建中差別:

用代碼創建cell中的處理和nib一樣,注冊了cell就有系統接管并且可以用帶forIndexPath的方法,沒有注冊就要自己去實例化cell,不能用帶forIndexPath的方法

[tableView registerClass:XxxCell class] forCellReuseIdentifier:@"xxCell"];

?

xib方式創建:

// 注冊Identifier

- (void)viewDidLoad{

??? [super viewDidLoad];

??? /**

???? 注意:以下幾句注冊XIB的代碼,一定要在viewDidLoad中!

???? 注冊XIB文件,獲得根視圖,并且轉換成TableView,為tableView注冊xib

???? Identifier名要在xib文件中定義,并且保持一致

???? **/

??? UINib *nib = [UINib nibWithNibName:@"BookCell" bundle:[NSBundle mainBundle]];

??? UITableView *tableView = (UITableView *)self.view;

??? [tableView registerNib:nib forCellReuseIdentifier:@"bookCell"];???

}

?

// 沒有注冊Identifier只能使用下面方法

static NSString *CellIdentifier = @"bookCell";

BookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

??????? cell = [[BookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

??????? NSBundle *bundle = [NSBundle mainBundle];

??????? NSArray *array = [bundle loadNibNamed:@"BookCell" owner:nil options:nil];

??????? cell = [array lastObject];

}

?

?

代碼方式創建:

1.建立UITableViewCell的類,繼承UITableViewCell

2.往cell里面加入view的時候注意點:

// 新建的組件放入contentView中

[self.contentView addSubview:xxView];

?

// 設置圖片拉伸屬性stretch

UIImage *normalImage = [UIImage imageNamed:@"xx.png"];

normalImage = [normalImage stretchableImageWithLeftCapWidth:

normalImage.size.width / 2 topCapHeight:normalImage.size.height / 2];

?

// 在tableView里面viewDiDLoad里面要注冊cell類

[tableView registerClass:XxxCell class] forCellReuseIdentifier:@"xxCell"];

?

自定義表格中Header

//自定義表格在這個方法中定義

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

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

?

轉載于:https://www.cnblogs.com/feife/p/4630367.html

總結

以上是生活随笔為你收集整理的简述UITableView的属性和用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。